安卓开发 3.4 网格布局

一、导读

  • 有些应用要求控件很整齐地排成若干行和若干列,当然利用线性布局嵌套是可以实现的,但是太繁琐了,我们可以采用网格布局来实现,就比较简单。

二、网格布局概述

1、布局特点

  1. GridLayout布局使用虚细线将布局划分为行、列和单元格,也支持一个控件在行、列上都有交错排列。
  2. 可以自己设置布局中组件的排列方式
  3. 可以自定义网格布局有多少行、多少列
  4. 可以直接设置组件位于某行某列
  5. 可以设置组件横跨几行或者几列

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>
  • 但在第五行添加五个按钮,但是第五个按钮跨两行,高度要重新设置
    在这里插入图片描述
  • 但第六行添加三个按钮,第一个按钮跨两列,宽度要重新设置
    在这里插入图片描述

5、启动应用,查看效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值