目录
一、导读
- 有些应用要求控件很整齐地排成若干行和若干列,当然利用线性布局嵌套是可以实现的,但是太繁琐了,我们可以采用网格布局来实现,就比较简单。
二、网格布局概述
1、布局特点
- GridLayout布局使用虚细线将布局划分为行、列和单元格,也支持一个控件在行、列上都有交错排列。
- 可以自己设置布局中组件的排列方式
- 可以自定义网格布局有多少行、多少列
- 可以直接设置组件位于某行某列
- 可以设置组件横跨几行或者几列
2、继承关系图
- GridLayout类是ViewGroup子类
3、常用属性
(1)针对布局的属性
属性 | 含义 |
---|---|
rowCount | 行数 |
columnCount | 列数 |
layout_width | 布局宽度 |
layout_height | 布局高度 |
(2)针对子控件的属性
属性 | 含义 |
---|---|
layout_row | 子控件在布局的行数 |
layout_column | 子控件在布局的列数 |
layout_rowSpan | 跨行数 |
layout_columnSpan | 跨列数 |
三、案例演示:计算器界面
1、创建安卓应用
- 基于
Empty Activity
模板创建安卓应用 -GridLayoutCalculator
2、字符串资源文件
- 字符串资源文件 -
strings.xml
3、自定义边框配置文件
- 在drawable目录里添加
custom_border.xml
- 在
Root element
更改为shape
点击ok,里面代码如下:
4、主布局资源文件
- 主布局资源文件 -
activity_main.xml
将默认的约束布局修改为垂直的线性布局,设置相关属性
- 添加显示运算结果的标签,并设置相关属性
- 添加一个网格布局,设置为6行5列
- 添加第一行的五个按钮
- 效果如下
- 剩下也是依次操作,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="15dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_border"
android:text="0123456789"
android:gravity="right"
android:textColor="#0000ff"
android:layout_marginTop="30dp"
android:textSize="20sp"/>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:columnCount="5"
android:rowCount="6" >
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="MC"
android:layout_row="0"
android:layout_column="0"
android:layout_marginRight="5dp"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="MR"
android:layout_marginRight="5dp"
android:layout_row="0"
android:layout_column="1"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="MS"
android:layout_marginRight="5dp"
android:layout_row="0"
android:layout_column="2"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="M+"
android:layout_marginRight="5dp"
android:layout_row="0"
android:layout_column="3"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="M-"
android:layout_marginRight="5dp"
android:layout_row="0"
android:layout_column="4"
android:textSize="15sp"/>
<!--/第二排/-->
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="<-"
android:layout_row="1"
android:layout_column="0"
android:layout_marginRight="5dp"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="CE"
android:layout_marginRight="5dp"
android:layout_row="1"
android:layout_column="1"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="C"
android:layout_marginRight="5dp"
android:layout_row="1"
android:layout_column="2"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="+_"
android:layout_marginRight="5dp"
android:layout_row="1"
android:layout_column="3"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="根号"
android:layout_marginRight="5dp"
android:layout_row="1"
android:layout_column="4"
android:textSize="15sp"/>
<!--第三排-->
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="7"
android:layout_row="2"
android:layout_column="0"
android:layout_marginRight="5dp"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="8"
android:layout_marginRight="5dp"
android:layout_row="2"
android:layout_column="1"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="9"
android:layout_marginRight="5dp"
android:layout_row="2"
android:layout_column="2"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="/"
android:layout_marginRight="5dp"
android:layout_row="2"
android:layout_column="3"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="%"
android:layout_marginRight="5dp"
android:layout_row="2"
android:layout_column="4"
android:textSize="15sp"/>
<!--/第四排///-->
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="4"
android:layout_row="3"
android:layout_column="0"
android:layout_marginRight="5dp"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="5"
android:layout_marginRight="5dp"
android:layout_row="3"
android:layout_column="1"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="6"
android:layout_marginRight="5dp"
android:layout_row="3"
android:layout_column="2"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="*"
android:layout_marginRight="5dp"
android:layout_row="3"
android:layout_column="3"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="1/X"
android:layout_marginRight="5dp"
android:layout_row="3"
android:layout_column="4"
android:textSize="15sp"/>
<!--/第五排/-->
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="1"
android:layout_row="4"
android:layout_column="0"
android:layout_marginRight="5dp"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="2"
android:layout_marginRight="5dp"
android:layout_row="4"
android:layout_column="1"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="3"
android:layout_marginRight="5dp"
android:layout_row="4"
android:layout_column="2"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="-"
android:layout_marginRight="5dp"
android:layout_row="4"
android:layout_column="3"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="100dp"
android:text="="
android:layout_marginRight="5dp"
android:layout_row="4"
android:layout_column="4"
android:layout_rowSpan="2"
android:textSize="15sp"/>
<!--第六排/-->
<Button
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="0"
android:layout_row="5"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_marginRight="5dp"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="."
android:layout_marginRight="5dp"
android:layout_row="5"
android:layout_column="2"
android:textSize="15sp"/>
<Button
android:layout_width="68dp"
android:layout_height="wrap_content"
android:text="+"
android:layout_marginRight="5dp"
android:layout_row="5"
android:layout_column="3"
android:textSize="15sp"/>
</GridLayout>
</LinearLayout>
- 但在第五行添加五个按钮,但是第五个按钮跨两行,高度要重新设置
- 但第六行添加三个按钮,第一个按钮跨两列,宽度要重新设置