Centering two views horizontally under a different view in ConstraintLayout

I have 3 buttons, one full-width button on top of two half-width buttons on the screen. Here is the layout:

<?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">
    <Button
        android:id="@+id/btn_save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Save"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/btn_delete"
        app:layout_constraintVertical_chainStyle="packed"/>
    <Button
        android:id="@+id/btn_delete"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Delete"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/btn_cancel"
        app:layout_constraintTop_toBottomOf="@+id/btn_save" />
    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Cancel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/btn_delete"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_save"/>
</android.support.constraint.ConstraintLayout>

As a result, the cancel button is canceled, and not aligned horizontally with the delete button. enter image description here

After playing around a bit so that the cancel button is horizontally aligned with the delete button, I had to add one of the following cancel buttons.

app:layout_constraintBaseline_toBaselineOf="@+id/btn_delete"

or

app:layout_constraintVertical_bias="0.0"

Questions:

  • Why did ConstraintLayout press the cancel button instead of aligning with the delete button? Why should I use the baseline or offset on the cancel button to align it one line at a time?

  • Besides using the baseline and offset, are there other ways for the cancel button to match the delete button?

+4
2

ConstraintLayout ? , ?

cancel , 2 cancel

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"

cancel save .

, delete , , cancel?? , save

app:layout_constraintBottom_toTopOf="@+id/btn_delete"

app:layout_constraintBottom_toTopOf="@+id/btn_cancel"

delete , cancel save.

app:layout_constraintBottom_toBottomOf="parent"

enter image description here

, delete cancel . , delete cancel

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_save"

save .

, ?

-

app:layout_constraintBottom_toBottomOf="parent" 

cancel delete

app:layout_constraintBottom_toTopOf="@+id/btn_delete"

app:layout_constraintBottom_toBottomOf="parent"

save.

enter image description here

, bottom constraint cancel delete save .

+3

app:layout_constraintBottom_toBottomOf="parent" btn_cancel

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

        <Button
            android:id="@+id/btn_save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Save"
            app:layout_constraintBottom_toTopOf="@+id/btn_delete"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Delete"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/btn_cancel"
            app:layout_constraintTop_toBottomOf="@+id/btn_save" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Cancel"
            app:layout_constraintLeft_toRightOf="@+id/btn_delete"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn_save" />
    </android.support.constraint.ConstraintLayout>

app:layout_constraintVertical_chainStyle="packed" chainStyle https://developer.android.com/training/constraint-layout/index.html#constrain-chain

+3

Source: https://habr.com/ru/post/1693295/


All Articles