Android布局总结一:GridLayout布局(网格布局)
GridLayout布局简介
GridLayout布局是Android4.0(API Level 14)新引入的网格矩阵形式的布局控件。
GridLayout属性介绍
本身属性
- android:alignmentMode
说明:当设置alignMargins,使视图的外边界之间进行校准。可以取以下值:
alignBounds – 对齐子视图边界。
alignMargins – 对齐子视距内容。 - android:columnCount
说明:GridLayout的最大列数 - android:rowCount
说明:GridLayout的最大行数 - android:columnOrderPreserved
说明:当设置为true,使列边界显示的顺序和列索引的顺序相同。默认是true。 - android:orientation
说明:GridLayout中子元素的布局方向。有以下取值:
horizontal – 水平布局。
vertical – 竖直布局。 - android:rowOrderPreserved
说明:当设置为true,使行边界显示的顺序和行索引的顺序相同。默认是true。 - android:useDefaultMargins
说明: 当设置ture,当没有指定视图的布局参数时,告诉GridLayout使用默认的边距。默认值是false。
子元素属性
- android:layout_column
说明:显示该子控件的列,例如android:layout_column=”0”,表示当前子控件显示在第1列,android:layout_column=”1”,表示当前子控件显示在第2列。 android:layout_columnSpan
说明:该控件所占的列数,例如android:layout_columnSpan=”2”,表示当前子控件占两列。android:layout_row
说明:显示该子控件的行,例如android:layout_row=”0”,表示当前子控件显示在第1行,android:layout_row=”1”,表示当前子控件显示在第2行。android:layout_rowSpan
说明:该控件所占的列数,例如android:layout_rowSpan=”2”,表示当前子控件占两行。android:layout_columnWeight
说明:该控件的列权重,与android:layout_weight类似,例如有GridLayout上两列,都设置android:layout_columnWeight = “1”,则两列各占GridLayout宽度的一半- android:layout_rowWeight
说明:该控件的行权重,原理同android:layout_columnWeight。
平均分配格行/列的问题
GridLayout在Android 5.1(API Level 21)时引入了android:layout_columnWeight和android:layout_rowWeight来解决平分问题,但是API21前怎么办呢?
1.需要用到兼容包:
compile 'com.android.support:gridlayout-v7:22.+'
2.布局中使用android.support.v7.widget.GridLayout
...
3.使用app:layout_columnWeight 和app:layout_rowWeight 设置权重
android:layout_rowSpan
android:layout_columnSpan
android:layout_columnWeight
android:layout_rowWeight
android:rowCount
android:columnCount
用
android:layout_rowSpan
android:layout_columnSpan
android:layout_columnWeight
android:layout_rowWeight
android:rowCout
android:columnCount
替换
4.举例:
<android.support.v7.widget.GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="10" app:rowCount="2" app:columnCount="2"> <LinearLayout android:background="#ffffff" android:layout_margin="1dp" android:orientation="vertical" app:layout_rowWeight="1" app:layout_columnWeight="1" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/logo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置"/> LinearLayout> <LinearLayout android:orientation="vertical" android:layout_margin="1dp" app:layout_rowWeight="1" app:layout_columnWeight="1" android:gravity="center" android:background="#ffffff"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/logo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="帮助"/> LinearLayout> <LinearLayout android:background="#ffffff" android:layout_margin="1dp" android:orientation="vertical" app:layout_rowWeight="1" app:layout_columnWeight="1" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/logo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退出"/> LinearLayout> <LinearLayout android:background="#ffffff" android:layout_margin="1dp" android:orientation="vertical" app:layout_rowWeight="1" app:layout_columnWeight="1" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/logo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐藏"/> LinearLayout> android.support.v7.widget.GridLayout>
更多相关文章
- android UI学习 -- 设置界面的布局(包括style的使用,selector的
- Android-Layout:RelativeLayout常用XML属性
- Android 属性动画(Property Animation) 完全解析【附源码】
- 【Android】界面布局之TableLayout(表格布局)
- Android布局编写示例【慢慢更新】
- Android 布局优化之include与merge
- 线性布局(LinearLayout)下layout_weight用法分析