首页 > 解决方案 > 无法在约束布局中添加第二个屏障

问题描述

我想创建一个约束布局,我可以在其中添加多个障碍,但它不起作用。

最后,我希望有一个类似于设置页面的布局,其中元素彼此位于下方,但是当我单击某些元素时,它下方会出现一些额外的元素,其余元素会向下移动。

我的方法是使用水平障碍并设置附加元素的可见性,但即使在下面这样的简单示例中我也无法添加第二个障碍(第二个障碍粘在顶部): 示例图片

布局文件:

<?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">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="Button"
    app:layout_constraintStart_toStartOf="parent"
    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="button"
    tools:layout_editor_absoluteY="731dp" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="Button"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/barrier" />

<android.support.constraint.Barrier
    android:id="@+id/barrier2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:barrierDirection="bottom"
    app:constraint_referenced_ids="button2"
    tools:layout_editor_absoluteY="731dp" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:text="Button"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="299dp" />

</android.support.constraint.ConstraintLayout>

知道如何在布局中放置多个障碍或创建这样的布局吗?

标签: androidlayoutandroid-constraintlayoutbarrier

解决方案


推荐阅读