ScrollView中ConstraintLayout中障碍的不良行为

时间:2017-12-22 10:59:16

标签: android-layout android-scrollview android-constraintlayout

我正在使用ConstraintLayout的最新版本,1.1.0-beta4,当ConstraintLayout位于ScrollView内时,我遇到了障碍问题。这是布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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.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="wrap_content"
        tools:context="rs.agilesolutions.anothertesttodelete.MainActivity"
        >

        <Button
            android:id="@+id/button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginTop="8dp"
            android:text="Button"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="Lorem"
            android:textSize="40sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/button"
            />

        <TextView
            android:id="@+id/txt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:text="Ipsum"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button"/>

        <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="txt1,txt2"
            />

        <TextView
            android:id="@+id/txtResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="UNDER BARRIER"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/barrier"/>

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="#00FF00"
            android:ems="10"
            android:hint="Name"
            android:inputType="textPersonName"
            android:text="Name"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txtResult"
            />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="#FF0000"
            android:ems="10"
            android:hint="password"
            android:inputType="textPassword"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText"
            />
    </android.support.constraint.ConstraintLayout>
</ScrollView>

屏障的方向设置为两个视图的bottomtxt1txt2)。它应该是这样的:

The way it should look like

,但相反,它看起来像这样:

The way it looks like

首先,在我的真实应用中,我有很多观点,这就是ScrollView需要的原因。

其次,按下按钮我出于演示原因更改txt(具有 Lorem 文本的那个)的可见性,从而测试较低视图的行为是否正常。

第三,很明显,因此,ConstraintLayout没有考虑到在这些视图下,引用了哪个障碍,它可以传播到足以包含其他内容,相反,它只是填充已经创建的从底部看剩余视图的布局空间。

这是ConstraintLayout实施中的错误吗?

如果没有我在代码中更改此类情况的约束并且无论视图的可见性如何,我还有其他选择吗?我相应地设置剩余视图的位置?

1 个答案:

答案 0 :(得分:2)

这是版本1.1.0-beta4中的错误。我将版本降低到beta3,一切都按预期工作。