您的位置:首页 > 移动开发 > Android开发

Android布局——GridLayout(网格布局)

2017-07-18 09:13 453 查看

说明

网格布局是4.0之后添加的布局,跟TableLayout有点像,但更加好用,它把容器分为一个rows*columns的网格,每个网格都是一个组件位,可是通过设置让组件位占据多行/列。

与之相似地,还有一个叫做GridView的组件,无论功能和名称都很相似,不过GridView使用Adapter来填充组件位,GridLayout则要简化得多

GridLayout本身的属性

属性对应方法属性说明
android:columnCountsetColumCount(int)设置布局的最大列数
android:rowCountsetRowCount(int)设置布局的最大行数
android:alignmentModesetAilgnmentMode(int)设置布局的对其方式(alignBounds: 对齐子视图边界;alignMargins: 对齐子视图边距;)
android:columnOrderPeservedsetColumOrderPreserved(boolean)设置容器是否保留列序号
android:rowOrderPeservedsetRowOrderPeserved(boolean)设置容器是否保留行序号
android:useDefaultMarginssetUseDefaultMargins(boolean)设置容器是否使用默认的页边距

针对容器内的子组件的属性

属性对应方法属性说明
android:layout_GravitysetGravity(int)设置组件如何占据其所属网格的空间
android:layout_column设置组件在容器的第几列
android:layout_row设置组件在容器的第几行
android:layout_columnSpan设置组件占据了几列
android:layout_rowSpan设置组件占据了几行
这里的LayoutGravity跟一般的LayoutGravity有点区别,这里的摆放位置是相对于所属网格的,而不是对于布局父容器来说的

栗子



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"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: