Android ConstraintLayout:三个按钮在宽度上均匀分布

Ati*_*min 11 android

我有一个ConstraintLayout水平包含3个按钮.我希望3个按钮具有固定的宽度,并在布局的宽度上均匀分布.

Sur*_*gch 50

这是一个视觉示例.

  1. 选择视图
  2. 右键单击并选择" 链">"创建水平链"

在此输入图像描述

也可以看看

  • 如果按钮的宽度不同,这不起作用 (2认同)

Pha*_*inh 13

要使 3 个视图在宽度上均匀分布,只需设置start/end每个视图的约束必须定义正确

代码

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent"
        />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintStart_toEndOf="@+id/button1"
        />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button2"
        />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

输出

更多
如果您需要全宽 3 个视图,只需更改 android:layout_width="wrap_content"android:layout_width="0dp"

输出