简体   繁体   English

Android Constraint Layout Barrier 和 Gone Margin

[英]Android Constraint Layout Barrier and Gone Margin

I have a constraint layout looks like this:我有一个约束布局如下所示:

A-B
--------- (barrier that refer to A and B bottom)
C (constraint_top_bottomOf barrier)

Here is the code:这是代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TypographyActivity"
    android:background="?attr/lightPrimary">

    <TextView
        android:id="@+id/textviewA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/textviewB"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Text A" />

    <TextView
        android:id="@+id/textviewB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toEndOf="@id/textviewA"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Text B" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textviewA, textviewB" />

    <TextView
        android:id="@+id/textviewC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_goneMarginTop="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/barrier"
        android:text="Text C" />

</androidx.constraintlayout.widget.ConstraintLayout>

goneMarginTop in textviewC always triggered even when A and B are visible.即使在 A 和 B 可见时,textviewC 中的 GoneMarginTop 也始终触发。

Is there a way I can add goneMarginTop to textviewC, when A and B are gone without wrapping A and B to another layout?当 A 和 B 消失而不将 A 和 B 包装到另一个布局时,有没有一种方法可以将 GoneMarginTop 添加到 textviewC 中?

Thank you.谢谢你。

Barriers do not show in layouts and, I believe, they are set to "gone" always.障碍不会出现在布局中,我相信,它们总是被设置为“消失”。 That is why you are seeing the gone margin on text view "C" - it is constrained to a "gone" widget.这就是为什么您会在文本视图“C”上看到消失的边距 - 它被限制为“消失”的小部件。

If you want to use a gone margin, you will have to constraint text view "C" to a widget that is gone when text views "A" and "B" are both gone.如果您想使用消失的边距,则必须将文本视图“C”约束为当文本视图“A”和“B”都消失时消失的小部件。 One way to do this is to create a Space widget and constrain its top to the barrier.一种方法是创建一个Space小部件并将其顶部限制在障碍物上。 Then constrain the top of "C" to the Space widget.然后将“C”的顶部约束到Space小部件。 You will have to ensure that the Space widget is gone when both "A" and "B" are gone and visible when either "A" or "B" (or both) is visible.当“A”或“B”(或两者)都可见时,您必须确保当“A”和“B”都消失并且可见时空间小部件消失。

The details will depend upon your needs, but the general layout will look something like this:细节将取决于您的需求,但总体布局将如下所示:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light">

    <TextView
        android:id="@+id/textviewA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Text A"
        android:visibility="visible"
        app:layout_constraintEnd_toStartOf="@+id/textviewB"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textviewB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Text B"
        android:visibility="visible"
        app:layout_constraintStart_toEndOf="@id/textviewA"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textviewA, textviewB" />

    <Space
        android:id="@+id/space"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/barrier" />

    <TextView
        android:id="@+id/textviewC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Text C"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/space"
        app:layout_goneMarginTop="30dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM