Android 网格布局 - 单元格均匀分布

Android Grid Layout - Even distribution of cells

我正在使用网格布局在屏幕上显示自定义视图。 我需要在水平方向上等距显示这些按钮以填充并适合屏幕。如果一行中可以容纳 X 个数字,则将其余的移动到另一行。以类似的方式添加新的动态视图。

类似于 Nexus 5 上的下图

但是当我检查较小屏幕的 ui 时,第 2 列和第 3 列消失了

如何在网格布局中实现图标的一致均匀分布?

使用的代码:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:columnCount="3"
android:paddingLeft="7dp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:paddingRight="3dp"
tools:context=".MainActivityFragment">

<com.custom.myview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_columnWeight="1"
    android:layout_margin="2dp"/>

<com.custom.myview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_columnWeight="1"
    android:layout_margin="2dp"/>

<com.custom.myview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_columnWeight="1"
    android:layout_margin="2dp"/>

<com.custom.myview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_columnWeight="1"
    android:layout_margin="2dp" />

我让它工作了,并且能够使用

根据屏幕大小调整列
<integer name="column_numbers">3</integer>

<integer name="column_numbers">2</integer>

在不同的值文件夹中,例如 values-normal-hdpi 或 values-normal-xxhdpi

在我的布局文件中添加 android:columnCount="@integer/column_numbers" 以获取相应屏幕尺寸的正确列数。