Android 4.0新组件:GridLayout详细说明

本文介绍Android 4.0新增组件GridLayout,该组件继承自LinearLayout,用于实现网格布局。GridLayout支持指定行数、列数及子视图跨行跨列显示等功能,简化了复杂布局的开发流程并提高性能。

于Android 4.0(API 14)它提供了一个新的组件GridLayout,它继承自Linearlayout,用于执行网络格样式布局。

在某些方面,GridLayout与TableLayout和GridView有相似去处。于能够指定每个单元格“横跨”几个单元格或者“竖跨”几个单元格。这一点与html中<table>标签非常类似。

GridLayout的几个重要属性:

rowCount:行数

columnCount:列数

GridLayout的子View将能够应用属性:

layout_rowSpan:纵向跨几个单元格

layout_columnSpan:横向跨几个单元格

同一时候。GridLayout的子View能够不指定layout_width和layout_height(类似于TableLayout)


使用GridLayout能够非常方便的开发出类似计算器的页面。相比使用LinearLayout简化了代码、简化了嵌套层次、提高了性能,而且自适应性能更好。

示意图:

   

布局代码:

<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="8dp" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="计算器" /> <GridLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:rowCount="5" android:columnCount="4" android:layout_margin="4dp"> <Button android:text="C" /> <Button android:text="Del" /> <Button android:text="/" /> <Button android:text="x" /> <Button android:text="7" /> <Button android:text="8" /> <Button android:text="9" /> <Button android:text="-" /> <Button android:text="4" /> <Button android:text="5" /> <Button android:text="6" /> <Button android:text="+" /> <Button android:text="1" /> <Button android:text="2" /> <Button android:text="3" /> <Button android:text="=" android:layout_gravity="fill" android:layout_rowSpan="2" /> <Button android:text="0" android:layout_gravity="fill" android:layout_columnSpan="2" /> <Button android:text="." /> </GridLayout> </LinearLayout>



版权声明:本文博主原创文章。博客,未经同意不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值