UI组件——GridLayout

本文详细介绍了Android 4.0中引入的GridLayout布局管理器。GridLayout能够将界面划分为多个行和列,并允许子视图跨多行或多列显示。文章解释了如何通过XML属性设置网格的行数、列数及对齐模式等关键配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 GridLayout(网格布局) 在Android4.0开始引入。
GridLayout可以把布局分为几行和几列。
GridLayout可以设置一个组件横框多少行,纵夸多少列。
GridLayout和TableLayout很相似,但是如果要设计行列布局,GridLayout使用更方便、更高效。因为:
GridLayout  works with a flat-view hierarchy, where child views set their locations in the grid by specifying the rows and columns they should be in. By maintaining a flat hierarchy, GridLayout is able to more swiftly layout its child views. 
The new GridLayout control for Android 4.0 is very powerful and we've just scratched the surface of what it can do

2 GridLayout常见属性
GridLayout常用的XML属性和方法说明
XML属性 相关方法 说明
android:alignmentMode setAlignmentMode(int) 设置该布局管理器采用的对齐模式
android:rowCount
setRowCount(int)
设置该网格的行数
android:columnCount setColumnCount(int) 设置该网格的列数量
android:rowOrderPreserved
setRowOrderPreserved(boolean)
设置该网格容器是否保留行序号
android:columnOrderPreserved setColumnOrderPreserved(boolean) 设置该网格容器是否保留序列号
android:useDefaultMargins setUseDefaultMargins(boolean) 设置该布局管理器是否使用默认的页边距


GridLayout.LayoutParams常用的XML属性和方法说明
XML属性
相关方法 
说明
android:layout_column
设置该组件在GridLayout的第几列
android:layout_columnSpan
设置该子组件在GridLayout横向上跨几列
android:layout_gravity
设置该子组件采用何种方式占据该网格的空间
android:layout_row
设置该子组件在GridLayout的第几行
android:layout_rowSpan
设置该子组件在GridLayout纵向上跨几行
android:orientation
说明:
(1) Gridlayout 的默认对齐方式是 android:orientation="horizontal"。
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"    
        android:rowCount="2"
        android:columnCount="2">
     <TextView
            android:text="Cell 0"
            android:textSize="14dip" />
     <TextView
            android:text="Cell 1"
            android:textSize="14dip" />
     <TextView
            android:text="Cell 2"
            android:textSize="14dip" />
     <TextView
            android:text="Cell 3"
            android:textSize="14dip" />
</GridLayout>

The layout will adjust the row and column sizes so that the cells can fit their content, as illustrated by the following diagram:
 


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

Now, the GridLayout will position the cells from top to bottom in each column, instead of left to right, as shown below:
(2)  layout_gravity,android:layout_row , android:layout_column and layout_textSise等 attribute can also adjust the size。
(3) layout_gravity attribute can also adjust the size。

    <Button
        android:layout_columnSpan="2"
        android:text="0"/>

    <Button
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="="/>

Ref:

转载于:https://my.oschina.net/sarashare/blog/662036

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值