0

我有一个引用三个视图的障碍。这三个视图在彼此之下,如下所示:

---- View A

---- View B

---- Group 

--------- Barrier

---- View D

View D对 Barrier 有约束,View B 和 Group 在任何时候都可能成为View.GONE

此外,该最初是空的。我以编程方式扩展视图 B 下的视图,并将所有视图与组相关联。

问题是,当我将视图膨胀到组中时,视图 D不会移动并停留在视图 B 下方。

显示组、障碍和视图 D 的代码:

<androidx.constraintlayout.widget.Group
    android:id="@+id/group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/view_b" />

<androidx.constraintlayout.widget.Barrier
    android:id="@+id/header_barrier"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:barrierDirection="bottom"
    app:constraint_referenced_ids="group, view_a, view_b" />

<View
    android:id="@+id/bottom_shadow"
    android:layout_width="0dp"
    android:layout_height="@dimen/grid_8"
    android:background="@drawable/orders_shadow_white"
    app:layout_constraintBottom_toBottomOf="@id/barrier"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
4

1 回答 1

1

您在底部阴影视图的约束中犯了错误

它应该是layout_constraintTop_toBottomOf 而不是 layout_constraintBottom_toBottomOf

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:padding="10dp">

    <View
        android:id="@+id/view_a"
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:background="@android:color/holo_blue_dark"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view_b"
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:background="@android:color/holo_green_dark"
        app:layout_constraintStart_toStartOf="@+id/view_a"
        app:layout_constraintTop_toBottomOf="@+id/view_a" />

    <androidx.constraintlayout.widget.Group
        android:id="@+id/group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="view_b"
        app:layout_constraintTop_toBottomOf="@id/view_b" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/header_barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="group, view_a, view_b" />

    <View
        android:id="@+id/bottom_shadow"
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:background="@android:color/holo_orange_light"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header_barrier" />

</androidx.constraintlayout.widget.ConstraintLayout>
于 2020-09-15T06:48:16.067 回答