【问题标题】:ConstraintLayout Barrier not working properly when parent dimensions are set to wrap_content当父尺寸设置为 wrap_content 时,ConstraintLayout Barrier 无法正常工作
【发布时间】:2018-06-15 15:16:10
【问题描述】:

这是我的问题的复制:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@id/textView2"
        android:text="Text"/>
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp"
        android:text="muchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertext"/>
    
    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textView, textView2"/>
    
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/barrier"
        android:text="muchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertext"/>
    
</android.support.constraint.ConstraintLayout>

当您将父高度设置为match_parent 时,屏障会按预期工作。但是,一旦将其设置为 wrap_content,它就无法正确布局。

下面是wrap_content 的样子,右边是match_parent

如果有人能指出我正确的方向,将不胜感激。我没有发现有人反对以这种方式使用 Barriers,但也没有发现任何人让这种布局起作用。

这是我的错误还是 Barrier 中的错误?

【问题讨论】:

    标签: android android-layout android-constraintlayout constraintlayout-barrier


    【解决方案1】:

    不清楚您所说的“父母”是什么意思。如果我将ConstraintLayout(我能看到的唯一父级)的高度设置为match_parent,则没有任何变化。结果就是你的第一张照片。

    你要做的,就是让屏障阻挡底部textView3,完成textViewtextView2barrier之间的垂直链。代码将如下所示:

    <?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="wrap_content">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Text"
            app:layout_constraintBottom_toTopOf="@id/barrier"
            app:layout_constraintEnd_toStartOf="@id/textView2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="muchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertext"
            app:layout_constraintBottom_toTopOf="@id/barrier"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/textView"
            app:layout_constraintTop_toTopOf="parent" />
    
        <android.support.constraint.Barrier
            android:id="@+id/barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="bottom"
            app:constraint_referenced_ids="textView, textView2" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="muchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertextmuchlongertext"
            app:layout_constraintTop_toBottomOf="@id/barrier" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 但是如何保持较小的顶部文本视图与约束布局的顶部对齐?
    • 没关系。我刚刚将 vertical_bias 设置为 0。感谢您的解决方案!
    • 我遇到了同样的问题。你是如何将垂直偏差设置为 0 的?
    猜你喜欢
    • 2018-10-08
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2018-04-19
    • 2016-11-23
    • 2017-08-13
    相关资源
    最近更新 更多