如何在 ConstraintLayout 中以固定边距和动态宽度均匀放置视图?

How to put views evenly with fixed margins and dynamic widths in ConstraintLayout?

我想这样设置 ConstraintLayout:

我需要有固定的左右约束,大小尺寸比为 1:1 和灵活的宽度,它会调整到屏幕的实际宽度。我设置如下:

  1. 左右约束均为8dp
  2. 这些视图的大小设置为 "Any size"
  3. layout_constraintDimensionRatio = "1:1"

我所拥有的是所有视图都设置为点,除了设置了左右约束的视图(其他视图只有左约束)。那一个应用了1:1比率的规则。

我应该怎么做才能修复此布局?

这是我尝试添加链的 xml:

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="rs.agilesolutions.constraintlayouttest.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:text="1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp"
        style="@style/TextViewCustom"
        app:layout_constraintRight_toLeftOf="@+id/textView2"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:id="@+id/textView2"
        android:text="2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView"
        style="@style/TextViewCustom"
        app:layout_constraintRight_toLeftOf="@+id/textView3"
        app:layout_constraintLeft_toRightOf="@+id/textView"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:id="@+id/textView3"
        android:text="3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView"
        style="@style/TextViewCustom"
        app:layout_constraintRight_toLeftOf="@+id/textView4"
        app:layout_constraintLeft_toRightOf="@+id/textView2"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:id="@+id/textView4"
        android:text="4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView"
        style="@style/TextViewCustom"
        app:layout_constraintRight_toLeftOf="@+id/textView5"
        app:layout_constraintLeft_toRightOf="@+id/textView3"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:id="@+id/textView5"
        android:text="5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView4"
        style="@style/TextViewCustom"
        app:layout_constraintDimensionRatio="1:1" />

</android.support.constraint.ConstraintLayout>

这是结果:

很接近,但还不够好。

您可以创建 chains 以确保相同的分布(例如每行一个链)——这样每个小部件将具有相同的尺寸。你描述的其余部分应该是正确的。

编辑:这是一些XML。诀窍是只在第一个元素上设置边距。

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView"
        android:text="1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintRight_toLeftOf="@+id/textView2"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView2"
        android:text="2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintRight_toLeftOf="@+id/textView3"
        app:layout_constraintLeft_toRightOf="@+id/textView"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView3"
        android:text="3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintRight_toLeftOf="@+id/textView4"
        app:layout_constraintLeft_toRightOf="@+id/textView2"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView4"
        android:text="4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintRight_toLeftOf="@+id/textView5"
        app:layout_constraintLeft_toRightOf="@+id/textView3"
        app:layout_constraintDimensionRatio="1:1" />

    <TextView
        android:background="@color/colorAccent"
        android:id="@+id/textView5"
        android:text="5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView4"
        app:layout_constraintDimensionRatio="1:1" />

</android.support.constraint.ConstraintLayout>