Как я могу сделать GridLayout универсальным для всех устройств? - PullRequest
0 голосов
/ 27 августа 2018

Я работаю над XML-действием с 12 кнопками в GridLayout (3x4) и хочу, чтобы эти кнопки были равномерно распределены (если это правильный термин) на экране для всех устройств.

Вот код XML

<?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"
tools:context=".UnitsActivity">

<android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:columnCount="3"
    app:rowCount="4">

    <Button
        android:id="@+id/unit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_one_button"
        app:layout_column="0"
        app:layout_row="0" />

    <Button
        android:id="@+id/unit2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_two_button"
        app:layout_column="1"
        app:layout_row="0" />

    <Button
        android:id="@+id/unit3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_three_button"
        app:layout_column="2"
        app:layout_row="0" />

    <Button
        android:id="@+id/unit4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_four_button"
        app:layout_column="0"
        app:layout_row="1" />

    <Button
        android:id="@+id/unit5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_five_button"
        app:layout_column="1"
        app:layout_row="1" />

    <Button
        android:id="@+id/unit6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_six_button"
        app:layout_column="2"
        app:layout_row="1" />

    <Button
        android:id="@+id/unit7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_seven_button"
        app:layout_column="0"
        app:layout_row="2" />

    <Button
        android:id="@+id/unit8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_eight_button"
        app:layout_column="1"
        app:layout_row="2" />

    <Button
        android:id="@+id/unit9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_nine_button"
        app:layout_column="2"
        app:layout_row="2" />

    <Button
        android:id="@+id/unit10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_ten_button"
        app:layout_column="0"
        app:layout_row="3" />

    <Button
        android:id="@+id/unit11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_eleven_button"
        app:layout_column="1"
        app:layout_row="3" />

    <Button
        android:id="@+id/unit12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/unit_twelve_button"
        app:layout_column="2"
        app:layout_row="3" />
</android.support.v7.widget.GridLayout>

Как видите, я использую Margin для их распространения, но это характерно для определенного устройства, в данном случае для Samsung Galaxy S7.

Ответы [ 2 ]

0 голосов
/ 27 августа 2018

Для использования равномерно распределенных кнопок, вы можете использовать Linear 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_margin="15dp"
        android:layout_weight="1">


        <Button
            android:id="@+id/unit1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3" />

        <Button
            android:id="@+id/unit2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3"
            />

        <Button
            android:id="@+id/unit3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_margin="15dp">


        <Button
            android:id="@+id/unit4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3"
            />

        <Button
            android:id="@+id/unit5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3"/>

        <Button
            android:id="@+id/unit6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_margin="15dp">


        <Button
            android:id="@+id/unit7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3" />

        <Button
            android:id="@+id/unit8"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3"/>

        <Button
            android:id="@+id/unit9"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_margin="15dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/unit10"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3" />

        <Button
            android:id="@+id/unit11"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3" />

        <Button
            android:id="@+id/unit12"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3"
            />

    </LinearLayout>


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

Надеюсь, это сработает для вас.

0 голосов
/ 27 августа 2018

Вы можете попробовать использовать:

android:layout_width="0dp"
android:weightSum="0.25"`
...