在展开链中绑定两个相邻的视图ConstraintLayout

时间:2017-03-28 20:52:17

标签: android-layout android-constraintlayout

我在ConstraintLayout的一个传播链中连接了一组六个小部件。此视图中没有其他布局,只有ConstraintLayout和小部件。

中心的两个小部件应该更加靠近,因为一个是另一个的标题:

widget-title
widget-EditText

我想在我的视图中的所有小部件上使用展开链(中心垂直),但我希望这两个特别是垂直打包在一起,同时尊重顶部边缘和顶部边距。最底层的。

我无法弄明白该怎么做。我尝试将它们作为垂直LinearLayout的子项放在一起,但它拒绝正确地参与传播链 - 它会到达顶部并且许多其他小部件都会消失。

我已经尝试向两个视图添加其他约束,但是传播链似乎会覆盖所有其他约束并且它们没有任何效果。唯一的例外是对齐他们的基线,它们可以工作但是将它们按在一起,这是我不想要的。

我没有看到在同一个ConstraintLayout中创建多个扩展链的方法,使它们均匀分布。

如何将链中的两个(或更多)小部件组合在一起,以使它们的传播规则与链的其他部分不同?或者失败了,你如何构建所有传播相同的链?

1 个答案:

答案 0 :(得分:1)

LinearLayout中映射两个中心小部件时,肯定会出现问题。以下布局就是这样,并且似乎按照我认为你想要它的方式工作。我没有在链中添加很多View,但它足以让我们了解这个想法。看看吧。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView1"
        app:layout_constraintBottom_toTopOf="@id/layout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread" />

    <LinearLayout
        android:id="@+id/layout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@id/textView4"
        app:layout_constraintTop_toBottomOf="@id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="TextView2" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="TextView3" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layout" />

</android.support.constraint.ConstraintLayout>