【问题标题】:ConstraintLayout Barrier not respecting GroupConstraintLayout 障碍不尊重组
【发布时间】:2020-09-15 14:48:16
【问题描述】:

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

---- View A

---- View B

---- Group 

--------- Barrier

---- View D

View D对Barrier有约束,View B和Group随时可能变成View.GONE

此外,Group 最初是空的。我以编程方式扩充视图 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" />

【问题讨论】:

  • 请添加整个代码并分享您想要的确切布局

标签: android android-constraintlayout constraintlayout-barrier


【解决方案1】:

您在bottom_shadow视图的约束中犯了错误。

应该是 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>

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 2019-09-06
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2015-05-04
    • 1970-01-01
    相关资源
    最近更新 更多