Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使GridLayout适用于所有设备?_Java_Android_Android Layout_Android Studio_Grid Layout - Fatal编程技术网

Java 如何使GridLayout适用于所有设备?

Java 如何使GridLayout适用于所有设备?,java,android,android-layout,android-studio,grid-layout,Java,Android,Android Layout,Android Studio,Grid Layout,我正在处理一个在GridLayout(3x4)中有12个按钮的XML活动,我希望这些按钮在所有设备的屏幕上均匀分布(如果这是正确的术语) 下面是XML代码 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://s

我正在处理一个在GridLayout(3x4)中有12个按钮的XML活动,我希望这些按钮在所有设备的屏幕上均匀分布(如果这是正确的术语)

下面是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>


希望,这会对你有用。

这不会影响布局,也许这会有帮助?谢谢,我曾想过这样做,但我看到一些帖子说这样做效率不高。然而,我认为对于一个简单的活动来说,这并不重要