如何使 GridLayout 对所有设备通用?

How can I make a GridLayout universal for all devices?

我正在开发一个 XML activity,在 GridLayout (3x4) 中有 12 个按钮,我希望这些按钮在屏幕上均匀分布(如果这是正确的术语)适用于所有设备。

这是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 来分发它们,但这是特定于特定设备的,在本例中为三星 Galaxy S7。

您可以尝试使用:

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

为了使用均匀分布的按钮,您可以为此使用线性布局。

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

希望这对你有用。