Android Studio gridlayout。预览和模拟器显示不同的结果

时间:2018-02-06 19:06:16

标签: android android-gridlayout

我正在尝试创建一个2列4行的简单网格布局按钮。我想要那些按钮填充他们的空间,就像它在预览中显示的那样。但模拟器显示不同的输出。

预览显示8个填充按钮,表示问候,但模拟器输出显示每个框左侧大小的按钮。

Preview

Emulator Results

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


<GridLayout


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnOrderPreserved="false"




    android:alignmentMode="alignMargins"
    android:columnCount="2"
    android:rowCount="4">

    <android.support.v7.widget.CardView

    android:layout_columnWeight="1"
    android:layout_rowWeight="1"


    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill"

        android:orientation="horizontal"

        >

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"
            android:text="hello"

            />



    </LinearLayout>


</android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView

        android:layout_columnWeight="1"
        android:layout_rowWeight="1"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"

            android:orientation="horizontal"

            >

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill"
                android:text="hello"

                />



        </LinearLayout>


    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView

        android:layout_columnWeight="1"
        android:layout_rowWeight="1"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"

            android:orientation="horizontal"

            >

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill"
                android:text="hello"

                />



        </LinearLayout>


    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView

        android:layout_columnWeight="1"
        android:layout_rowWeight="1"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"

            android:orientation="horizontal"

            >

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill"
                android:text="hello"

                />



        </LinearLayout>


    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView

        android:layout_columnWeight="1"
        android:layout_rowWeight="1"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"

            android:orientation="horizontal"

            >

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill"
                android:text="hello"

                />



        </LinearLayout>


    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView

        android:layout_columnWeight="1"
        android:layout_rowWeight="1"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"

            android:orientation="horizontal"

            >

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill"
                android:text="hello"

                />



        </LinearLayout>


    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView

        android:layout_columnWeight="1"
        android:layout_rowWeight="1"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"

            android:orientation="horizontal"

            >

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill"
                android:text="hello"

                />



        </LinearLayout>


    </android.support.v7.widget.CardView>
    <android.support.v7.widget.CardView

        android:layout_columnWeight="1"
        android:layout_rowWeight="1"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"

            android:orientation="horizontal"

            >

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill"
                android:text="hello"

                />



        </LinearLayout>


    </android.support.v7.widget.CardView>




</GridLayout>

1 个答案:

答案 0 :(得分:0)

在这种情况下,我无法解决问题,但我只使用了常规网格布局,每个按钮都有列和行指示。这就是诀窍。 请注意,我有layout_column和layout_row来指定它们应该站在哪里。

[解决方案] [1]:https://i.stack.imgur.com/K5MAx.png

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnOrderPreserved="false">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:text="hello"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="0"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:text="hello"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="1"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:text="hello"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="1"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:text="hello"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="2"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:text="hello"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="2"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:text="hello"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="3"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:text="hello"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="3"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:text="hello"
        />

   

相关问题