Android布局——GridLayout(网格布局)
2017-07-18 09:13
453 查看
说明
网格布局是4.0之后添加的布局,跟TableLayout有点像,但更加好用,它把容器分为一个rows*columns的网格,每个网格都是一个组件位,可是通过设置让组件位占据多行/列。与之相似地,还有一个叫做GridView的组件,无论功能和名称都很相似,不过GridView使用Adapter来填充组件位,GridLayout则要简化得多
GridLayout本身的属性
属性 | 对应方法 | 属性说明 |
---|---|---|
android:columnCount | setColumCount(int) | 设置布局的最大列数 |
android:rowCount | setRowCount(int) | 设置布局的最大行数 |
android:alignmentMode | setAilgnmentMode(int) | 设置布局的对其方式(alignBounds: 对齐子视图边界;alignMargins: 对齐子视图边距;) |
android:columnOrderPeserved | setColumOrderPreserved(boolean) | 设置容器是否保留列序号 |
android:rowOrderPeserved | setRowOrderPeserved(boolean) | 设置容器是否保留行序号 |
android:useDefaultMargins | setUseDefaultMargins(boolean) | 设置容器是否使用默认的页边距 |
针对容器内的子组件的属性
属性 | 对应方法 | 属性说明 |
---|---|---|
android:layout_Gravity | setGravity(int) | 设置组件如何占据其所属网格的空间 |
android:layout_column | 设置组件在容器的第几列 | |
android:layout_row | 设置组件在容器的第几行 | |
android:layout_columnSpan | 设置组件占据了几列 | |
android:layout_rowSpan | 设置组件占据了几行 |
栗子
xml文件
<?xml version="1.0" encoding="utf-8"?> <GridLayout android:columnCount="4" android:i 4000 d="@+id/root1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:layout_rowSpan="2" android:layout_width="100dp" android:layout_height="200dp" android:background="#600096" /> <ImageView android:layout_columnSpan="2" android:layout_width="200dp" android:layout_height="100dp" android:background="#006096" /> <ImageView android:layout_rowSpan="2" android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="bottom" android:background="#009660" /> <ImageView android:layout_columnSpan="2" android:layout_rowSpan="2" android:layout_width="200dp" android:layout_height="300dp" android:background="#960060" /> <ImageView android:layout_width="100dp" android:layout_height="200dp" android:background="#AA6060" /> <ImageView android:layout_width="100dp" android:layout_height="200dp" android:background="#AA55AA" /> </GridLayout>
栗子2
xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4"> <!--6行4列 实现占满整个屏幕--> <EditText android:hint="数值" android:layout_columnSpan="4" android:layout_gravity="fill_horizontal" android:layout_rowWeight="2" /> <!--跨四列 自动填充 权重2--> <Button android:text="清除" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp" android:textColor="#00F"/> //列 行权重为1 <Button android:text="后退" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="/" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="x" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="7" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="8" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="9" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="-" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="4" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="5" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="6" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="+" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="1" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="2" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="3" android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行权重为1 <Button android:text="=" android:layout_rowSpan="2" android:layout_gravity="fill_vertical" android:layout_columnWeight="1" android:layout_rowWeight="2" android:textSize="20dp" android:background="#22ac38"/> //跨两行 自动填充 绿色 列权重1 行权重2 <Button android:text="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" android:layout_columnWeight="2" android:layout_rowWeight="1" android:textSize="20dp"/> //跨两列 自动填充 列权重2 行权重1 <Button android:text="." android:layout_columnWeight="1" android:layout_rowWeight="1" android:textSize="20dp"/> //列 行 权重1 </GridLayout>
解决API<21时 GridLayout平均分配格行/列的问题
问题:GridLayout在API21时引入了Android:layout_columnWeight和android:layout_rowWeight来解决平分问题,但是api21前就不可以使用,这是需要引入兼容包点击相应的AppCompat控件,下载即可
使用这个GridLayout如下:
<android.support.v7.widget.GridLayout ...... />
使GridLayout的子元素平分屏幕
需要用下面的这两个属性
app:layout_columnWeight="1" app:layout_rowWeight="1"
相关文章推荐
- Android基础入门教程——2.2.5 GridLayout(网格布局)
- Android零基础入门第32节:新推出的GridLayout网格布局
- android之网格布局GridLayout
- 第16章、布局Layouts之GridLayout网格布局(从零开始学Android)
- Android学习 9-> 表格布局TableLayout和网格布局GridLayout
- Android零基础入门第32节:新推出的GridLayout网格布局
- android之网格布局GridLayout
- [置顶] Android--(9)--详解网格布局(GridLayout)
- 【Android】7.3 GridLayout(网格布局)
- Android实现计算器布局(四种布局方式)之GridLayout网格布局
- Android开发7:布局管理器4(网格布局GridLayout,绝对布局AbsoluteLayout)
- 第16章、布局Layouts之GridLayout网格布局(从零开始学Android)
- android之网格布局GridLayout
- android 4.0引入的新布局GridLayout(网格布局)
- Android中使用GridLayout网格布局来制作简单的计算器App
- Android UI之GridLayout(网格布局)
- Android基础入门教程——2.2.5 GridLayout(网格布局)
- Android布局总结一:GridLayout布局(网格布局)
- Android布局之GridLayout网格布局