如何定位在ConstraintLayout中两个视图中最低的位置之下?

Zak*_*rdi 53 android android-layout android-support-library android-constraintlayout

我有两个标题视图,HeaderViewAHeaderViewB.这些视图可以具有任何可见性visible或组合gone.

我需要BigView被定位最低的任一HeaderViewA/ HeaderViewB.

这可能没有嵌套ConstraintLayout吗?

在此输入图像描述

Eug*_*sov 80

现在,在约束布局v1.1.0中引入了Barrier类.

所以这是您特定情况的解决方案:

<?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"
    tools:context="com.example.eugene.test10.MainActivity">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#EEEEEE"
        android:text="TEXT_VIEW_1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/textView2"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#DDDDDD"
        android:text="TEXT_VIEW_2"
        android:visibility="gone"
        app:layout_constraintLeft_toRightOf="@+id/textView1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Barrier
        android:id="@+id/labelBarrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textView1,textView2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#CCCC00"
        android:text="TEXT_VIEW_3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/labelBarrier" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

以下是使用此布局的结果:

在模拟器上查看

您可以参考Codelab https://codelabs.developers.google.com/codelabs/constraint-layout/#10上的此分步指南.

  • @Zerato你需要将`'com.android.support.constraint:constraint-layout:1.1.0-beta3'`依赖项添加到你的模块的gradle文件中,并将maven存储库添加到项目的gradle文件中.您可以在此GitHub项目中找到所有依赖项https://github.com/googlecodelabs/constraint-layout (2认同)
  • androidx 的“androidx.constraintlayout.widget.Barrier” (2认同)