一般网站的跳出率广州疫情防控措施
虽然线性布局既能在水平方向排列,也能在垂直方向排列,但它不支持多行多列的布局方式,只支持单行(水平排列)或单列(垂直排列)的布局方式。若要实现类似表格那样的多行多列形式,可采用网格 布局GridLayout网格布局默认从左往右、从上到下排列,它先从第一行从左往右放置下级视图,塞满之后另起一行放置 其余的下级视图,如此循环往复直至所有下级视图都放置完毕。为了判断能够容纳几行几列,网格布局 新增了android:columnCount与android:rowCount两个属性,
一:columnCount指定了网格的列数, 即每行能放多少个视图;
二:rowCount指定了网格的行数,即每列能放多少个视图。
下面是运用网格布局的XML布局样例,它规定了一个两行两列的网格布局,且内部容纳四个文本视图。
XML文件内容如下所示:
<!-- 根布局为两行两列的网格布局,其中列数由columnCount指定,行数由rowCount指定 --><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:columnCount="2"android:rowCount="2"><TextViewandroid:layout_width="180dp"android:layout_height="60dp"android:gravity="center"android:background="#ffcccc"android:text="浅红色"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:layout_width="180dp"android:layout_height="60dp"android:gravity="center"android:background="#ffaa00"android:text="橙色"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:layout_width="180dp"android:layout_height="60dp"android:gravity="center"android:background="#00ff00"android:text="绿色"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:layout_width="180dp"android:layout_height="60dp"android:gravity="center"android:background="#660066"android:text="深紫色"android:textColor="#000000"android:textSize="17sp" />
</GridLayout>
在一个新建的活动页面加载上述布局,运行App观察到的界面如下图所示:
由上图可见,App界面的第一行分布着浅红色背景与橙色背景的文本视图,第二行分布着绿色背景与 深紫色背景的文本视图,说明利用网格布局实现了多行多列的效果。
运行前记得修改清单文件
完整代码:
Java:
package com.example.chapter03;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class GridLayoutActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_grid_layout);}
}
布局文件:
<?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"android:columnCount="2"android:rowCount="2"><TextViewandroid:layout_width="0dp"android:layout_columnWeight="1"android:layout_height="60dp"android:background="#ffcccc"android:text="浅红色"android:gravity="center"android:textColor="#000000"android:textSize="17dp"/><TextViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_columnWeight="1"android:background="#ffaa00"android:text="橙色"android:gravity="center"android:textColor="#000000"android:textSize="17dp"/><TextViewandroid:layout_width="0dp"android:layout_height="60dp"android:layout_columnWeight="1"android:background="#00ff00"android:text="绿色"android:gravity="center"android:textColor="#000000"android:textSize="17dp"/><TextViewandroid:layout_width="0dp"android:layout_columnWeight="1"android:layout_height="60dp"android:background="#660066"android:text="深紫色"android:gravity="center"android:textColor="#000000"android:textSize="17dp"/></GridLayout>
感谢观看!!!