Android 如何在ConstraintLayout中的两个视图中的最低视图下方定位?
我有两个标题视图,Android 如何在ConstraintLayout中的两个视图中的最低视图下方定位?,android,android-layout,android-support-library,android-constraintlayout,Android,Android Layout,Android Support Library,Android Constraintlayout,我有两个标题视图,HeaderViewA和HeaderViewB。这些视图可以具有可见性可见或消失的任意组合 我需要将BigView定位在HeaderViewA/HeaderViewB中最低的下 无需嵌套在ConstraintLayout中,这是否可行 对于app:layout\u constraintWidth\u default=“wrap”(宽度设置为0dp)。如果设置,小部件的大小将与使用wrap_内容时相同,但会受到约束(即,它不会扩展到它们之外)和app:layout_constr
HeaderViewA
和HeaderViewB
。这些视图可以具有可见性可见
或消失
的任意组合
我需要将BigView
定位在HeaderViewA
/HeaderViewB
中最低的下
无需嵌套在ConstraintLayout
中,这是否可行
对于app:layout\u constraintWidth\u default=“wrap”
(宽度设置为0dp
)。如果设置,小部件的大小将与使用wrap_内容时相同,但会受到约束(即,它不会扩展到它们之外)和app:layout_constraintVertical_bias=“1.0”
的限制。使用“偏移”将BigView拉到其父视图的底部。现在可以使用Barrier类,该类在constraint layout v1.1.0中引入
下面是针对您的具体案例的解决方案:
<?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>
你可以参考这个关于Codelab的分步指南。我很想听到一个不同的答案,但恐怕这是不可能的。很有趣,但仅仅通过XML看起来是不可能的。您不需要嵌套视图,但如果您对编程操作持开放态度,则可以使用。为什么嵌套约束会破坏交易?@AlexMeuer嵌套布局会对性能产生负面影响(请参阅此报告)。因此,这是使布局尽可能平坦的主要目标,ConstraintLayout具有所有必要的功能。@EugeneBrusov,我发现。我相信我的评论是在屏障功能发布之前。对于类似的问题有一点不同的看法。在我的项目中,我需要先做些什么来实现屏障吗?@Zerato您需要将'com.android.support.constraint:constraint layout:1.1.0-beta3'
依赖项添加到模块的gradle文件中,并将maven存储库添加到项目的gradle文件中。您可以在这个GitHub项目中找到那些希望通过GUI工具访问这些屏障的人的所有依赖项,它不能通过textview、imageview等进行搜索,而是可以在I-beam外观的图标下拉列表中找到,该下拉列表中有指导线。itsandroidx.constraintlayout.widget.Barrier
用于androidxapp:I_love\u这些=“android_x,约束_布局”/>
<?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>