ConstraintLayout内可以链接(添加关系)多少个视图

时间:2019-04-29 13:50:23

标签: android android-constraintlayout constraint-layout-chains

Android中的约束布局支持视图链接(https://developer.android.com/reference/android/support/constraint/ConstraintLayout#chain-style

我检查了几个示例,到处仅展示了3个视图来演示该概念。

极限是3吗?

我尝试添加第4个元素,但根据概念未将其链接在一起。 Chaining with 3 Elements

具有3个元素的链接

Chaining with 4 elements

具有4个元素的链条

代码-https://gist.github.com/vamsigp/784be59f8ae1e2137ece1349c726d1f8

2 个答案:

答案 0 :(得分:2)

您的布局有误:

app:layout_constraintRight_toRightOf="share2"

应该是:

app:layout_constraintRight_toLeftOf="@+id/share2"

答案 1 :(得分:1)

3不是限制,您可以将4个或更多视图链接在一起。

以下是4个视图链的示例:

<?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">

<Button
    android:id="@+id/button9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/button10"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button12"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toStartOf="@+id/button11"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button9"
    app:layout_constraintTop_toTopOf="@+id/button9" />

<Button
    android:id="@+id/button11"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button10"
    app:layout_constraintTop_toTopOf="@+id/button9" />

<Button
    android:id="@+id/button12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toStartOf="@+id/button9"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button9" />
</android.support.constraint.ConstraintLayout>

它运行良好:

enter image description here

因此,链条非常灵活,例如,这里有9个视图链:

<?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">

<Button
    android:id="@+id/button9"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/button13"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button13"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toStartOf="@+id/button14"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button9"
    app:layout_constraintTop_toTopOf="@+id/button9" />

<Button
    android:id="@+id/button14"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toStartOf="@+id/button15"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button13"
    app:layout_constraintTop_toTopOf="@+id/button9" />

<Button
    android:id="@+id/button15"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toStartOf="@+id/button16"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button14"
    app:layout_constraintTop_toTopOf="@+id/button9" />

<Button
    android:id="@+id/button16"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toStartOf="@+id/button17"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button15"
    app:layout_constraintTop_toTopOf="@+id/button9" />

<Button
    android:id="@+id/button17"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toStartOf="@+id/button18"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button16"
    app:layout_constraintTop_toTopOf="@+id/button9" />

<Button
    android:id="@+id/button18"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toStartOf="@+id/button19"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button17"
    app:layout_constraintTop_toTopOf="@+id/button9" />

<Button
    android:id="@+id/button19"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toStartOf="@+id/button20"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button18"
    app:layout_constraintTop_toTopOf="@+id/button9" />

<Button
    android:id="@+id/button20"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="@+id/button9"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button19"
    app:layout_constraintTop_toTopOf="@+id/button9" />

</android.support.constraint.ConstraintLayout>

就像它看起来不错之前一样:

enter image description here

您甚至可以在有很多视图以至于看起来像条纹的情况下使用它(我不会共享该XML,因为您明白了):

enter image description here