将所有子宽度与 ConstraintLayout 中的最宽子宽度匹配,宽度 = 包装内容

HBB*_*B20 7 android android-layout android-constraintlayout

ConstraintLayout 功能强大,但有时也很棘手。

我想实现一个布局ConstraintLayout,可以通过LinearLayout.

  • 蓝色区域是父约束布局。红色部分是LinearLayout。我想通过保持以下条件将此 LinearLayout 转换为 ConstraintLayout

    1. 所有子项(按钮)的宽度应与最宽的子项匹配。
    2. 最宽的孩子不是固定的。它将在运行时更改。
    3. 红框应该保持不变wrap_content

我曾尝试使用障碍、指南和约束布局的其他属性,但没有成功。

这是代码LinearLayout

<android.support.constraint.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"
android:background="#476dce"
android:padding="16dp">

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="#f5aaaa">


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:text="Small" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:text="Bigggggeer" />


    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:text="Even Bigggggggggeer" />

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

Kri*_*rma -2

这是修改后的布局ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:background="#476dce"
    android:padding="16dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f5aaaa"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            tools:text="Small" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintTop_toBottomOf="@id/button1"
            tools:text="Bigggggeer" />


        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintTop_toBottomOf="@id/button2"
            tools:text="Even Bigggggggggeer" />

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

  • 将 LinearLayout 更改为 ConstraintLayout 并不会使其成为 ConstraintLayout 的方式。它仍然是嵌套的,并且违背了库的创建目的。另外,匹配父母将不起作用。CL 支持匹配约束 (0dp)。 (2认同)