问题
I'm having an issue to spread-chain 2 groups of elements with Constraint Layout. I understand the goal of this new layout is to use a flat hierarchy so I'd like to avoid putting my elements inside a child layouts.
I looked at some awesome resources like constraintlayout.com but couldn't figure out how to make it work for my specific case - which I think can be common..
Here is an image of what I'd like to achieve. In red, spaces 1, 2 and 3 need to have the same height (just like spread chain).
Thank you for your attention :)
回答1:
You can achieve this using:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button2"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="1dp"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button4"
app:layout_constraintTop_toBottomOf="@+id/button3"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/button4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
来源:https://stackoverflow.com/questions/55820502/spread-chain-group-of-elements-with-constraintlayout