【问题标题】:layout weight in Constraint layout约束布局中的布局权重
【发布时间】:2018-09-10 18:06:18
【问题描述】:

我是约束布局概念的新手。我想创建一个连续 3 个视图的布局。 2 视图应显示没有任何重量,第 3 视图应占据屏幕的第二半。基本上我想将下面的代码转换为约束布局。 此外,当第三个视图可见性消失时,剩余的两个视图应该占据所有宽度。

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_weight="1">

        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="he" />

        <Button
            android:id="@+id/b4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="he" />



    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:layout_weight="1">


        <Button
            android:id="@+id/b3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="hello2" />

    </LinearLayout>
</LinearLayout>

我在下面添加了截图:

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    根据this,设置宽度百分比可以使用app:layout_constraintWidth_percent="your percent"

    所以你可以使用这个:

    <?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"
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
    
        <Button
            android:id="@+id/b1"
            android:layout_width="0"
            android:layout_height="wrap_content"
            android:text="he"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.25" />
    
        <Button
            android:id="@+id/b4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="he"
            app:layout_constraintBottom_toBottomOf="@+id/b1"
            app:layout_constraintStart_toEndOf="@+id/b1"
            app:layout_constraintTop_toTopOf="@+id/b1"
            app:layout_constraintVertical_bias="1.0"
            app:layout_constraintWidth_percent="0.25" />
    
        <Button
            android:id="@+id/b3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="hello2"
            app:layout_constraintBottom_toBottomOf="@+id/b4"
            app:layout_constraintStart_toEndOf="@+id/b4"
            app:layout_constraintTop_toTopOf="@+id/b4"
            app:layout_constraintWidth_percent="0.5" />
    
    </android.support.constraint.ConstraintLayout>
    

    还可以观看these videos了解更多信息。

    更新:

    如果你想为一个按钮使用app:layout_constraintWidth_percent,你可以为一个按钮设置它,然后为另一个按钮设置`android:layout_width="0"。

    您也可以使用chain:观看视频的第 3 部分

    【讨论】:

    • 我不想将任何 layout_constraintWidth_percent 放在 b1 和 b4 上,将 50% 放在 b3 上。有可能吗?
    【解决方案2】:

    ConstraintLayout 确实支持LinearLayout 的权重概念,只要您使用“链”,但我认为在您的情况下,您最好使用Guideline 来定位半角视图.

    <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">
    
        <Button
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HE"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    
        <Button
            android:id="@+id/two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HE"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@+id/one"/>
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5"/>
    
        <Button
            android:id="@+id/three"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="HELLO2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@+id/guideline"
            app:layout_constraintEnd_toEndOf="parent"/>
    
    </android.support.constraint.ConstraintLayout>
    

    Guideline 的app:layout_constraintGuide_percent 属性告诉它将自己定位在屏幕任一边缘的中间位置(您可以在此处使用01 之间的任何值)。 android:orientation 属性告诉它画一条垂直线(而不是水平线)。

    指南到位后,您可以将其他视图约束到它,就像使用任何其他视图类型一样。

    【讨论】:

    • 我还有一个要求。如果 Guideline 和 Hello2 按钮的可见性消失了,那么两个按钮应该占据整个屏幕宽度
    • 在您的原始帖子中,前两个按钮的宽度为 wrap_content。您需要按钮占据全屏,还是只需要按钮后面的背景