注:网格布局通常在recyclerview的java中运用
一、网格布局的常见属性
1.android:columnCount 设置水平显示还是垂直显示<br /> 2.android:columnCount 设置行的显示个数<br /> 3.android:rowCount 设置列的显示个数
子控件属性
1.android:layout_column 显示在第几列<br /> 2.android:layout_columnSpan 横向跨几列<br /> 3.android:layout_columnWeight 横向剩余空间分配方式<br /> 4.android:layout_gravity 在网格中的显示位置<br /> 5.android:layout_row 显示在第几行<br /> 6.android:layout_rowSpan 横向跨几行<br /> 7.android:layout_rowWeight 纵向剩余空间分配方式
二、网格布局的实现
1.布局activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="3" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第1个" android:layout_columnSpan="2" android:layout_gravity="fill" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第2个" android:layout_row="1" android:layout_column="2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第3个" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第4个" android:layout_columnWeight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第5个" /></GridLayout>
2.效果图:<br />