2025年从零开始学android编程之网格布局管理器(2-1)

从零开始学android编程之网格布局管理器(2-1)网格布局管理器用 GridLayout 类来表示 在 从零开始学 android 编程之表格布局管理器 中提到的 TableLayout 一般产生的表格外形是标准的方框 而 GridLayout 类产生的网格可以是不标准的 1 设置网格的行数和列数 在 从零开始学 android 编程之线性布局管理器 中提到的 activity linear xml 文件中使用表格布局管理器 GridLayout 代码如下

网格布局管理器用GridLayout类来表示。在《从零开始学android编程之表格布局管理器》中提到的TableLayout一般产生的表格外形是标准的方框,而GridLayout类产生的网格可以是不标准的。

1 设置网格的行数和列数

在《从零开始学android编程之线性布局管理器》中提到的activity_linear.xml文件中使用表格布局管理器GridLayout,代码如下

android:orientation=“vertical”

………
>
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”>
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“按键1″/>
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“按键2″/>
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“按键3″/>
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“按键4″/>
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“按键5″/>
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“按键6″/>

在表格布局管理器GridLayout中添加了6个Button组件,效果如图1所示。从图1可以看出,在未设置表格布局管理器的行数和列数时,该管理器的效果与线性布局管理器的横向排列效果相同。

可以通过表格布局管理器GridLayout的android:columnCount属性来设置表格的列数,通过android:rowCount属性设置表格的行数。需要注意的是,在使用android:columnCount属性时,必须将表格布局管理器的android:orietation属性设置为horizontal;而使用android:rowCount属性时必须将android:orietation属性设置为vertical。代码如下所示

………
android:orientation=“vertical”
android:rowCount=“3”
>

以上代码将6个Button组件分成了3×2的网格,如图2所示。

编程小号
上一篇 2025-02-08 11:01
下一篇 2025-01-27 18:30

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/145887.html