使用权重居中的 GridLayout 和框

GridLayout and boxes centred using weights

我尝试使用 GridLayout 在中心生成 4 个相等的框,但我得到的是以下内容:

我一定是做错了什么。我的代码不应该至少填满屏幕吗?这是我的代码:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnCount="8"
            android:rowCount="8">

    <Button
            android:layout_columnSpan="4"
            android:layout_rowSpan="4"
            android:layout_rowWeight="4"
            android:layout_columnWeight="4"
            android:text="1" />

    <Button
            android:layout_columnSpan="4"
            android:layout_rowSpan="4"
            android:layout_rowWeight="4"
            android:layout_columnWeight="4"
            android:text="2" />

    <Button
            android:layout_columnSpan="4"
            android:layout_rowSpan="4"
            android:layout_rowWeight="4"
            android:layout_columnWeight="4"
            android:text="3" />

    <Button
            android:layout_columnSpan="4"
            android:layout_rowSpan="4"
            android:layout_rowWeight="4"
            android:layout_columnWeight="4"
            android:text="4" />
</GridLayout>

有谁知道可能遗漏了什么?

谢谢

PercentRelativeLayout 做得很好:

<android.support.percent.PercentRelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/button_s"
                app:layout_widthPercent="30%"
                app:layout_heightPercent="30%"
                app:layout_marginTopPercent="10%"
                app:layout_marginLeftPercent="15%"
                app:layout_marginRightPercent="5%"/>
        <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/button_c"
                app:layout_widthPercent="30%"
                app:layout_heightPercent="30%"
                app:layout_marginTopPercent="10%"
                app:layout_marginLeftPercent="5%"
                app:layout_marginRightPercent="15%"
                android:layout_toRightOf="@id/button_s"/>

        <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/button_r"
                app:layout_widthPercent="30%"
                app:layout_heightPercent="30%"
                app:layout_marginTopPercent="10%"
                app:layout_marginLeftPercent="15%"
                app:layout_marginRightPercent="5%"
                android:layout_below="@id/button_s"/>
        <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_widthPercent="30%"
                app:layout_heightPercent="30%"
                app:layout_marginTopPercent="10%"
                app:layout_marginLeftPercent="5%"
                app:layout_marginRightPercent="15%"
                android:layout_toRightOf="@id/button_r"
                android:layout_below="@id/button_c"/>

</android.support.percent.PercentRelativeLayout>

取决于你想要达到的目标:

  • 将按钮放在单元格的中央,添加

    android:layout_gravity="center"
    
  • 填写可用space,添加

    android:layout_gravity="fill"
    

android:layout_gravity for details. You can combine this with ViewGroup.MarginLayoutParams