Замените GridLayout на LinearLayout

У меня есть GridLayout, который отображает экран калькулятора. Он выглядит очень хорошо на многих устройствах, но совершенно не работает на маленьких дисплеях и даже на некоторых больших дисплеях. Есть ошибка в реализации GridLayout, но Google не работает над ней. Вы можете увидеть подробности в моем вопросе полугодовой давности и принятом ответе: GridLayout сворачивается на маленьком экране

Я попытался заново реализовать свой макет. Начал с нового ConstraintLayout без везения. LinearLayout довольно близко, но растянуть отдельные ячейки мне не удалось. Они не заполняют доступное пространство. Когда я меняю ширину с wrap_content на match_parent, макет полностью ломается.

Весь исходный код и образец приложения доступны по адресу https://github.com/literakl/CalcTrouble.

Текущая линейная компоновка

введите здесь описание изображения

GridLayout, когда он работает

введите здесь описание изображения

Сломанный GridLayout на Nexus 5

введите здесь описание изображения

Сломанный GridLayout на Nexus S

введите здесь описание изображения

Исходный код макета

<LinearLayout
android:id="@+id/calc_content"
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:background="@color/bg_calc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!--Display row-->
<TextView
    android:id="@+id/assignment"
    android:text="50 + 40 = 90"
    style="@style/Formula"
    android:focusable="false"
    android:layout_width="match_parent"
    android:layout_height="@dimen/calc_display_height"
    tools:ignore="HardcodedText" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- row with 7-9,+ -->
    <LinearLayout
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent">
    <Button
        android:id="@+id/digit7"
        android:text="7"
        style="@style/KeypadLeftButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText"  />

    <Button
        android:id="@+id/digit8"
        android:text="8"
        style="@style/KeypadButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText"  />

    <Button
        android:id="@+id/digit9"
        android:text="9"
        style="@style/KeypadButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/buttonPlus"
        android:text="+"
        style="@style/KeypadFunctionRightButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText" />
    </LinearLayout>

    <!--row with 4-6, - -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

    <Button
        android:id="@+id/digit4"
        android:text="4"
        style="@style/KeypadLeftButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/digit5"
        android:text="5"
        style="@style/KeypadButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/digit6"
        android:text="6"
        style="@style/KeypadButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText"  />

    <Button
        android:id="@+id/buttonMinus"
        android:text="-"
        style="@style/KeypadFunctionRightButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText" />
    </LinearLayout>

    <!--row with 1-3, * -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

    <Button
        android:id="@+id/digit1"
        android:text="1"
        style="@style/KeypadLeftButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText"  />

    <Button
        android:id="@+id/digit2"
        android:text="2"
        style="@style/KeypadButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText"  />

    <Button
        android:id="@+id/digit3"
        android:text="3"
        style="@style/KeypadButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/buttonMultiply"
        android:text="\u22C5"
        style="@style/KeypadFunctionRightButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText" />
    </LinearLayout>

    <!--row with 0, backspace and / -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

    <Button
        android:id="@+id/digit0"
        android:text="0"
        style="@style/KeypadLeftButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        tools:ignore="HardcodedText"  />

    <Button
        android:id="@+id/buttonBackspace"
        android:text="←"
        style="@style/KeypadFunctionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_columnSpan="2"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/buttonDivide"
        android:text=":"
        style="@style/KeypadFunctionRightButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        tools:ignore="HardcodedText" />
    </LinearLayout>
</LinearLayout>

<!--row with button submit -->

<Button
    android:id="@+id/buttonSubmit"
    android:text="@string/action_next_formula"
    style="@style/KeypadNextButton"
    android:layout_width="match_parent"
    android:layout_height="@dimen/calc_next_height"
    app:layout_gravity="fill_horizontal"/>

</LinearLayout>

Как добиться желаемого внешнего вида, который будет работать на всех устройствах?


person Leos Literak    schedule 18.08.2017    source источник
comment
как насчет использования вложенного линейного макета? родительский линейный макет с вертикальной ориентацией и вложенные макеты вкладышей с весом = 1 и высотой = 0. кнопки внутри внутреннего линейного макета должны иметь одинаковый вес с шириной = 0   -  person Swati    schedule 18.08.2017


Ответы (2)


Сделайте вашу кнопку width на 0dp, height на match_parent и layout_weight на 1, как в примере ниже для одной строки

    <!-- row with 7-9,+ -->
    <LinearLayout
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum ="4"
        android:orientation="horizontal"
        android:layout_width="match_parent">
    <Button
        android:id="@+id/digit7"
        android:text="7"
        style="@style/KeypadLeftButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:ignore="HardcodedText"  />

    <Button
        android:id="@+id/digit8"
        android:text="8"
        style="@style/KeypadButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:ignore="HardcodedText"  />

    <Button
        android:id="@+id/digit9"
        android:text="9"
        style="@style/KeypadButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/buttonPlus"
        android:text="+"
        style="@style/KeypadFunctionRightButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:ignore="HardcodedText" />
    </LinearLayout>
</LinearLayout>
person Rajan Kali    schedule 18.08.2017
comment
Могу я задать вопрос? Не отображается нижняя кнопка :-( - person Leos Literak; 21.08.2017
comment
Я только что поместил кнопку в LinearLayout, и теперь все в порядке - person Leos Literak; 21.08.2017

Используя ConstraintLayouts, вы можете добиться этого:

введите здесь описание изображения

Код макета:

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

    <Button
        android:id="@+id/button_top"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/button_bottom"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_marginBottom="9dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout_content"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        app:layout_constraintBottom_toTopOf="@+id/button_bottom"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_top">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/first_row"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            app:layout_constraintBottom_toTopOf="@+id/second_row"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <Button
                android:id="@+id/button_first_first"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/button_first_second"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_first_second"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/button_first_first"
                app:layout_constraintRight_toLeftOf="@+id/button_first_third"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_first_third"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/button_first_second"
                app:layout_constraintRight_toLeftOf="@+id/button_first_fourth"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_first_fourth"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/button_first_third"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/second_row"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            app:layout_constraintBottom_toTopOf="@+id/third_row"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/first_row">

            <Button
                android:id="@+id/button_second_first"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/button_second_second"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_second_second"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/button_second_first"
                app:layout_constraintRight_toLeftOf="@+id/button_second_third"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_second_third"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/button_second_second"
                app:layout_constraintRight_toLeftOf="@+id/button_second_fourth"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_second_fourth"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/button_second_third"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/third_row"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            app:layout_constraintBottom_toTopOf="@+id/fouth_row"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/second_row">

            <Button
                android:id="@+id/button_third_first"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/button_third_second"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_third_second"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/button_third_first"
                app:layout_constraintRight_toLeftOf="@+id/button_third_third"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_third_third"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/button_third_second"
                app:layout_constraintRight_toLeftOf="@+id/button_third_fourth"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_third_fourth"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/button_third_third"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/fouth_row"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/third_row">

            <Button
                android:id="@+id/button_fourth_first"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_weight="0.5"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/button_fourth_second"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_fourth_second"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintLeft_toRightOf="@+id/button_fourth_first"
                app:layout_constraintRight_toLeftOf="@+id/button_fourth_third"
                app:layout_constraintTop_toTopOf="parent"/>

            <Button
                android:id="@+id/button_fourth_third"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="2dp"
                android:layout_marginEnd="2dp"
                android:layout_marginStart="2dp"
                android:layout_marginTop="2dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_weight="0.5"
                app:layout_constraintLeft_toRightOf="@+id/button_fourth_second"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>

        </android.support.constraint.ConstraintLayout>
    </android.support.constraint.ConstraintLayout>


</android.support.constraint.ConstraintLayout>
person smora    schedule 18.08.2017
comment
Спасибо, я дам тебе попробовать - person Leos Literak; 18.08.2017