假设您有固定的宽度和高度,可以创建 3x3 单元格ConstraintLayout
,每个单元格具有相同的宽度和高度。这是代码:
<?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">
<TextView
android:id="@+id/textview_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@id/textview_4"
app:layout_constraintEnd_toStartOf="@id/textview_2"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread"
tools:text="TextView 1" />
<TextView
android:id="@+id/textview_2"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="@id/textview_1"
app:layout_constraintEnd_toStartOf="@id/textview_3"
app:layout_constraintStart_toEndOf="@id/textview_1"
app:layout_constraintTop_toTopOf="@id/textview_1"
tools:text="TextView 2" />
<TextView
android:id="@+id/textview_3"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="@id/textview_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/textview_2"
app:layout_constraintTop_toTopOf="@id/textview_1"
tools:text="TextView 3" />
<TextView
android:id="@+id/textview_4"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@id/textview_7"
app:layout_constraintEnd_toEndOf="@id/textview_1"
app:layout_constraintStart_toStartOf="@id/textview_1"
app:layout_constraintTop_toBottomOf="@id/textview_1"
tools:text="TextView 4" />
<TextView
android:id="@+id/textview_5"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="@id/textview_4"
app:layout_constraintEnd_toEndOf="@id/textview_2"
app:layout_constraintStart_toStartOf="@id/textview_2"
app:layout_constraintTop_toTopOf="@id/textview_4"
tools:text="TextView 5" />
<TextView
android:id="@+id/textview_6"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="@id/textview_4"
app:layout_constraintEnd_toEndOf="@id/textview_3"
app:layout_constraintStart_toStartOf="@id/textview_3"
app:layout_constraintTop_toTopOf="@id/textview_4"
tools:text="TextView 6" />
<TextView
android:id="@+id/textview_7"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/textview_1"
app:layout_constraintStart_toStartOf="@id/textview_1"
app:layout_constraintTop_toBottomOf="@id/textview_4"
tools:text="TextView 7" />
<TextView
android:id="@+id/textview_8"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="@id/textview_7"
app:layout_constraintEnd_toEndOf="@id/textview_2"
app:layout_constraintStart_toStartOf="@id/textview_2"
app:layout_constraintTop_toTopOf="@id/textview_7"
tools:text="TextView 8" />
<TextView
android:id="@+id/textview_9"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="@id/textview_7"
app:layout_constraintEnd_toEndOf="@id/textview_3"
app:layout_constraintStart_toStartOf="@id/textview_3"
app:layout_constraintTop_toTopOf="@id/textview_7"
tools:text="TextView 9" />
</android.support.constraint.ConstraintLayout>
这里在第一列创建了一个垂直链,在第一行创建了一个水平链。这样,您可以控制第一行中所有单元格的宽度,并控制第一列中所有单元格的高度。这也使您免于在布局中创建多个链,并且此布局中的所有链实际上是 2 仅驻留在第一个上TextView
,因此更易于管理。
我希望这会有所帮助,如果您对此有任何疑问,请告诉我。