效果图:
![](https://img-blog.csdnimg.cn/20210528122219991.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW5namluMTEyMA==,size_16,color_FFFFFF,t_70#pic_center)
关键代码:android:layout_width="0dp"
,0dp在ConstraintLayout中的含义是match_constraint
。
完整代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/rl_left"
android:layout_width="0dp"
android:layout_height="200dp"
android:background="#00ff00"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/rl_right"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:id="@+id/rl_right"
android:layout_width="0dp"
android:layout_height="200dp"
android:background="#ff0000"
app:layout_constraintLeft_toRightOf="@id/rl_left"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>