ConstraintLayout如何让同一横排的三个控件平分宽度
时间: 2024-05-08 08:11:20 浏览: 277
在ConstraintLayout中,可以使用`app:layout_constraintWidth_default="spread"`属性来让同一横排的三个控件平分宽度。具体实现步骤如下:
1.在XML布局文件中,将三个控件放在同一个水平链条中,并设置它们的左右约束。
2.为三个控件分别设置`app:layout_constraintWidth_default="spread"`属性。
下面是一个示例代码:
```xml
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button 1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/button2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="spread" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button 2"
app:layout_constraintLeft_toRightOf="@id/button1"
app:layout_constraintRight_toLeftOf="@id/button3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="spread" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button 3"
app:layout_constraintLeft_toRightOf="@id/button2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="spread" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
阅读全文
相关推荐


















