网格容器布局, 可以兼容的写出类似Excel表格一样的效果;
我平时项目开发必备框架
- Android上最强网络请求 Net
- Android上最强列表(包含StateLayout) BRV
- Android最强缺省页 StateLayout
- JSON和长文本日志打印工具 LogCat
- 支持异步和全局自定义的吐司工具 Tooltip
- 开发调试窗口工具 DebugKit
- 一行代码创建透明状态栏 StatusBar
GridLayout 属性
排列模式
android:alignmentMode
- alignBounds 对齐边界
- alignMargins 对齐margin
如果子视图存在margin(边距), 可以控制是否对齐margin还是对齐子视图的边界. padding无效
行数
android:columnCount 列数
android:rowCount 行数
超过行数或者列数就会自定换行
android:columnOrderPreserved
android:rowOrderPreserved
以上两个属性是控制行或者列的最大范围是否跟随最大值
android:orientation 方向
android:useDefaultMargins 默认边距
是否使用默认的边距(8dp), 默认false.
如果GridLayout不给子控件设置任何属性, 将默认从左到右/从上到下排列. 超过指定行列就换行
LayoutParams
GridLayout的每个网格都是一个Cell. 这些子控件时通过指定属性来指定其在网格中的位置
控制
默认不填写属性的情况下是依次排列行和列, 但是你可以通过手动指定行列控制排列(可以出现重叠)
android:layout_column 整数n,在哪一列开始显示n=[0, 最大列-1]
android:layout_row 指定从哪一行开始显示,规则同列数
示例:
如果我只指定android:layout_row="1", column依旧不变. 并且后面的子视图会根据Button2全部改变位置
如果我再指定android:layout_column="0"
如果同时修改为0, 会覆盖第一个子视图.
android:layout_row="0"
android:layout_column="0"
跨度
即控制一个cell应该拥有多大的跨度(范围)
android:layout_columnSpan 整数k,指定元素横跨几列,需要注意保证n+k <= 最大列数
android:layout_rowSpan 纵向跨几行,规则同列
示例:
<Button
android:layout_columnSpan="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
对齐方式
android:layout_gravity
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_columnSpan="2"
android:text="1"
/>
权重
android:layout_columnWeight
android:layout_rowWeight
示例:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_columnSpan="2"
android:text="2"
/>
示例
<GridLayout
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="200dp"
android:alignmentMode="alignMargins"
android:columnCount="4"
android:rowCount="4"
>
<Button
android:layout_width="80dp"
android:layout_height="50dp"
/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_column="2"
android:layout_row="1"
/>
<Button
android:layout_width="80dp"
android:layout_height="50dp"
/>
<Button
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_column="1"
android:layout_row="2"
/>
</GridLayout>
效果图
可以看出来通过子控件使用Column和Row属性可以指定其在网格中的位置