首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android Studio gridlayout。预览和仿真器显示不同的结果

Android Studio gridlayout。预览和仿真器显示不同的结果
EN

Stack Overflow用户
提问于 2018-02-07 03:06:16
回答 1查看 342关注 0票数 0

我试图创建一个2列和4行的简单网格布局按钮。我希望这些按钮填充它们的空间,就像它在预览中显示的那样。但是模拟器显示了不同的输出。

Preview恰好显示了8个充满问候的按钮,但模拟器的输出显示了每个框左侧大小的按钮。

Preview

Emulator Results

代码语言:javascript
运行
复制
<?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>

EN

Stack Overflow用户

发布于 2018-02-07 12:58:12

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

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

代码语言:javascript
运行
复制
<?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"
        />
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48650193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档