设置layout_weight权重为1。有权重的地方全部都为相等的比例。
使用ConstraintLayout 进行权重分配 app:layout_constraintHorizontal_chainStyle="spread_inside" 可以达到我们需要的效果。
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/but_A"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:background="#FF00FF"
app:layout_constraintEnd_toStartOf="@id/but_B"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/but_B"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFF00"
app:layout_constraintEnd_toStartOf="@id/but_C"
app:layout_constraintStart_toEndOf="@id/but_A"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/but_C"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#0000FF"
app:layout_constraintEnd_toStartOf="@id/but_D"
app:layout_constraintStart_toEndOf="@id/but_B"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/but_D"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="20dp"
android:background="#00FFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/but_C"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这个属性有三种样式
packed
spread
spread_inside
转载自https://blog.csdn.net/f1171531338/article/details/88627367