Как я могу использовать LinearLayout с делителями и гравитацией без элементов, выходящих за их границы? - PullRequest
0 голосов
/ 09 октября 2019

У меня есть следующий макет с использованием разделителей для расстояния между кнопками:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/row1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        android:divider="@drawable/divider_regular_buttons"
        android:orientation="horizontal"
        android:showDividers="middle|beginning|end"
        android:weightSum="2">

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_red_dark"
            android:text="Cancel"
            tools:visibility="visible" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_red_dark"
            android:text="Accept"
            tools:visibility="visible" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/row2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:divider="@drawable/divider_regular_buttons"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:showDividers="middle|beginning|end"
        android:weightSum="2">

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_red_dark"
            android:text="Cancel"
            tools:visibility="visible" />

        <Button
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_red_dark"
            android:text="Accept"
            tools:visibility="visible" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/row1b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        android:divider="@drawable/divider_regular_buttons"
        android:orientation="horizontal"
        android:showDividers="middle|beginning|end"
        android:weightSum="2">

        <Button
            android:id="@+id/button1b"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_red_dark"
            android:text="Cancel"
            tools:visibility="visible" />

        <Button
            android:id="@+id/button2b"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_red_dark"
            android:text="Accept"
            tools:visibility="gone" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/row2b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:divider="@drawable/divider_regular_buttons"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:showDividers="middle|beginning|end"
        android:weightSum="2">

        <Button
            android:id="@+id/button3b"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_red_dark"
            android:text="Cancel"
            tools:visibility="visible" />

        <Button
            android:id="@+id/button4b"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_red_dark"
            android:text="Accept"
            tools:visibility="gone" />

    </LinearLayout>

</LinearLayout>

Предварительный просмотр в Android Studio показывает следующее изображение:

enter image description here

В первом ряду показаны делители без гравитации и рендеринг, как и ожидалось. Второй ряд показывает делители с gravity="center_horizontal", и это портит строку, смещая ее вправо. Как я могу предотвратить это?

Причину, по которой я хочу использовать как центрирование, так и делители, можно увидеть в третьем и четвертом рядах: используя weightSum из 2, я могу сделать одну кнопку GONEи, благодаря gravity, кнопка будет центрироваться в середине (четвертый ряд). Но делители нарушают центрирование (и компоновку в целом), поэтому нет смысла, если мне приходится отслеживать обе вещи.

Есть ли способ заставить делители работать с гравитацией?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...