【问题标题】:ConstraintLayout issue with horizontal weight水平权重的 ConstraintLayout 问题
【发布时间】:2017-07-01 09:11:00
【问题描述】:

考虑以下 xml 布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/b1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="B1"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/b2"/>

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B2"
        app:layout_constraintLeft_toRightOf="@+id/b1"
        app:layout_constraintRight_toLeftOf="@id/b3"/>

    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B3"
        app:layout_constraintLeft_toRightOf="@+id/b2"
        app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>

这会产生我想要的布局:

但如果我将 B3 标记为已消失,则 B1 的宽度设置为 0,并且 B2 向左移动。而我希望 B2 取代 B3 并扩大 B1:

知道我做错了什么吗?

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    为 B2 设置 android:layout_width = "0dp"app:layout_constraintHorizontal_weight="1",您就可以出发了。

    编辑:只需从 B2 中删除 app:layout_constraintRight_toLeftOf="@id/b3"

    【讨论】:

    • 但这会改变 B2 的宽度,这不是我想要的。看第一张截图。我希望 B1 灵活,B2 和 B3 应该是wrap_content
    • 只需从 B2 中删除 app:layout_constraintRight_toLeftOf="@id/b3",因为 B3 视图设置为 GONE 可见性,布局混淆了将此 B2 视图保留在左侧的内容。抱歉,一开始我误解了你的问题。
    • 我想保持它的双向链。但无论如何,删除app:layout_constraintRight_toLeftOf ="@id/b3" 解决了我的问题。
    猜你喜欢
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 2017-11-20
    • 2017-08-24
    • 2014-02-10
    相关资源
    最近更新 更多