1.直接在里面写入按钮,默认横向显示
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="第1个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第2个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第3个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第4个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第5个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</GridLayout>
2.纵向显示
android:orientation="vertical"
3.设置行的显示个数
一行显示多少列
android:columnCount="3"
4.设置列的显示个数
两行代码同时作用,表示一列显示多少行
android:orientation="vertical"
android:rowCount="3"
5.行列的三个属性
显示在第几列
横向跨几列
横向剩余空间分配方式
第二个按钮显示在1行0列
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="第1个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第2个"
android:layout_row="1"
android:layout_column="0"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第3个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第4个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第5个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</GridLayout>
表示剩余空间由第五个按钮占用
<Button
android:text="第5个"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
横向剩余占用+纵向剩余占用
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="第1个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第2个"
android:layout_row="1"
android:layout_column="0"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第3个"
android:layout_row="0"
android:layout_column="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第4个"
android:layout_columnWeight="1" //这列上横向剩余占用
android:layout_rowWeight="1" //纵向剩余占用
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:text="第5个"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</GridLayout>
6.第二个按钮横跨三列
<Button
android:text="第2个"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="3" //两行代码同时作用
android:layout_gravity="fill" //在网络中的显示位置
android:layout_height="wrap_content"
android:layout_width="wrap_content" />