【问题标题】:GridLayout not evenly spacing children throughout screenGridLayout 没有在整个屏幕上均匀地间隔孩子
【发布时间】:2016-05-11 12:18:47
【问题描述】:

我有一个 GridLayout,里面有 6 个 LinearLayout 孩子。里面是按钮和其他东西。

在 Android Studio 中它看起来不错,但在实际设备(甚至模拟器)上它的空间并不均匀。这是正在发生的事情的图片。

<RelativeLayout <!--main layout stuff-->
>

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="4"
    android:columnCount="2">
        <!--
           there are six of these inside the grid layout
        -->
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1">

            <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/button"
                android:src="@drawable/button"
                android:layout_gravity="center"/>
        </LinearLayout>

     </GridLayout>

</RelativeLayout>

【问题讨论】:

  • 您在哪个设备上测试?
  • HTC One Mini、三星和模拟器
  • 我建议你使用Gridview自定义

标签: android xml android-linearlayout grid-layout android-relativelayout


【解决方案1】:

问题在于 android:layout_columnWeightandroid:layout_rowWeight 在 Lollipop 中是新的,因此在此之前不受支持。您可以使用support libraryapp:layout_columnWeight/app:layout_rowWeight 中的GridLayout 来为较旧的Android 设备获取这些功能。

<android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:rowCount="4"
    app:columnCount="2">
        <!--
           there are six of these inside the grid layout
        -->
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1">

            <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/button"
                android:src="@drawable/button"
                android:layout_gravity="center"/>
        </LinearLayout>

</GridLayout>

【讨论】:

    【解决方案2】:

    您需要将LinearLayout 的高度和宽度设置为0dp 以匹配父布局 看看这个

    <RelativeLayout <!--main layout stuff-->
    >
        <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:rowCount="4"
        android:columnCount="2">
            <!--
               there are six of these inside the grid layout
            -->
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1">
    
                <ImageButton
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/button"
                    android:src="@drawable/button"
                    android:layout_gravity="center"/>
            </LinearLayout>
    
         </GridLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 当我尝试它在 Android Studio 中看起来很完美,但是当我运行应用程序时,我现在只看到白色。
    【解决方案3】:
    <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/button"
            android:src="@drawable/drawer_bg"
            android:layout_gravity="center"
            android:scaleType="fitXY"/>
    

    在您的ImageButton 中尝试scaleTypefitXY

    【讨论】:

    • 不。现在它只是显示了当我运行应用程序时看不到网格子项的相对布局
    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2015-11-19
    • 1970-01-01
    相关资源
    最近更新 更多