今天刚好吃了没事,我突然想到今天早上我的语音助手说我这里空气质量不错,于是我就想要是有一个界面可以看看周围环境数据变化就好了,于是我就写了一个界面,用到了网格布局和相对布局的嵌套,大家可以试试:

首先新建一个activity,记得是空的,因为其他的有乱七八糟的东西,不适于我们学习;

我们将默认的约束布局改为网格布局:

关于网格布局,我这里就提一下几个重要的地方:

源代码:

<?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:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"tools:context=".MainActivity2"android:columnCount="3"android:layout_gravity="center_horizontal"android:rowCount="2"><RelativeLayoutandroid:layout_width="200dp"android:layout_height="100dp"android:layout_marginTop="10dp"android:layout_marginRight="20dp"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:text="空气温度"android:textSize="25sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:background="@drawable/myoral"android:layout_marginBottom="5dp"android:layout_marginRight="5dp"android:text="32"android:textSize="25sp"/></RelativeLayout><RelativeLayoutandroid:layout_width="200dp"android:layout_height="100dp"android:layout_marginTop="10dp"android:layout_marginRight="20dp"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:text="空气湿度"android:textSize="25sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginBottom="5dp"android:layout_marginRight="5dp"android:background="@drawable/myoval2"android:text="80"android:textSize="25sp"/></RelativeLayout><RelativeLayoutandroid:layout_width="200dp"android:layout_height="100dp"android:layout_marginTop="10dp"android:layout_marginRight="20dp"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:text="光照"android:textSize="25sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginBottom="5dp"android:layout_marginRight="5dp"android:background="@drawable/myoval2"android:text="158"android:textSize="25sp"/></RelativeLayout><RelativeLayoutandroid:layout_width="200dp"android:layout_height="100dp"android:layout_marginTop="10dp"android:layout_marginRight="20dp"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:text="CO2"android:textSize="25sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginBottom="5dp"android:layout_marginRight="5dp"android:background="@drawable/myoval2"android:text="32"android:textSize="25sp"/></RelativeLayout><RelativeLayoutandroid:layout_width="200dp"android:layout_height="100dp"android:layout_marginTop="10dp"android:layout_marginRight="20dp"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:text="PM2.5"android:textSize="25sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginBottom="5dp"android:layout_marginRight="5dp"android:background="@drawable/myoval2"android:text="180"android:textSize="25sp"/></RelativeLayout><RelativeLayoutandroid:layout_width="200dp"android:layout_height="100dp"android:layout_marginTop="10dp"android:layout_marginRight="20dp"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:text="道路状况"android:textSize="25sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:layout_alignParentRight="true"android:layout_alignParentBottom="true"android:layout_marginBottom="5dp"android:layout_marginRight="5dp"android:background="@drawable/myoval2"android:text="拥堵"android:textSize="25sp" /></RelativeLayout></GridLayout>

 别看这么长的代码,其实很多都可以复制粘贴的,主要的代码其实只有:

<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:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"tools:context=".MainActivity2"android:columnCount="3"android:layout_gravity="center_horizontal"android:rowCount="2"><RelativeLayoutandroid:layout_width="200dp"android:layout_height="100dp"android:layout_marginTop="10dp"android:layout_marginRight="20dp"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:text="空气温度"android:textSize="25sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:background="@drawable/myoral"android:layout_marginBottom="5dp"android:layout_marginRight="5dp"android:text="32"android:textSize="25sp"/></RelativeLayout>
</GridLayout>

关于网格布局的要点只有:

android:rowCount和android:columnCount,这是设置有多少行和多少列的,只要写上数值就可以开始界面的设计了;

我们添加一个 <RelativeLayout>相对布局,里面放了两个<TextView>,一个显示文字,一个显示数字;

为了好看,我将文字设置10dp的外边距:
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"

关于数字,我们需要放在右下角,同样我们用外边距隔开:

android:layout_alignParentBottom="true" 与父控件下对齐
            android:layout_alignParentRight="true" 与父控件右对齐
            android:background="@drawable/myoral"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="5dp"

这里用到了android:background="@drawable/myoral",其实这个是一个形状,是我自定义的形状,在drawable里面添加一个<shape>详细的请看专栏第一篇:

这里有专门讲如何创建一个形状:

(120条消息) Android <Textview>的高级应用:真的很高级(提高篇)_编程学渣ズ的博客-CSDN博客

当做好第一个之后是这样的:

剩下的几个只需要复制粘贴,傻瓜式操作的填写数据就行了;

是不是很简单?

关注我,更多分享,公众号:代码栈

Android:空气质量检测界面(布局嵌套),练手推荐。相关推荐

  1. 基于单片机的室内空气质量检测系统(甲烷甲醛)

    设计简介: 本设计是基于单片机的室内空气质量检测系统,主要实现以下功能: 可通过LCD1602显示当前甲烷和甲醛的值: 可通过按键调整甲醛甲烷的最大值: 可通过MS1100检测甲醛的值: 可通过MQ- ...

  2. DIY 空气质量检测表

    DIY 空气质量检测表 前几天逛淘宝看到有空气颗粒物浓度测量的传感器,直接是 3.3V TTL 电压串口输出的,也不贵,也就 100 多一点.觉得挺好就买了个,这两天自己捣鼓了个小程序,搞了个软件界面 ...

  3. ESP32开发之旅——基于ESP32的室内空气质量检测系统

    ESP32开发之旅--基于ESP32的室内空气质量检测系统 前言 成果展示 核心代码 MQTT部分 获取传感器数据部分 连接WiFi部分 结尾 前言 本文主要是我课设项目的一个备忘,部分资料来源于网上 ...

  4. js逆向 空气质量检测平台

    js逆向 空气质量检测平台 郑重声明 郑重声明:本项目的所有代码和相关文章, 仅用于经验技术交流分享,禁止将相关技术应用到不正当途径,因为滥用技术产生的风险与本人无关. 反调试绕过 url:https ...

  5. 基于stm32单片机的空气质量检测仿真(仿真+源码+全套资料)

    资料编号:072 可以通过STM32的ADC检测当前的空气质量,LCD1602显示,并且可以自动控制风扇排气,也可以设置为手动排气.包含全套资料,下面是视频演示: 72-基于stm32单片机的空气质量 ...

  6. java基于ssm空气质量检测系统源码网站空气质量监测源码

    简介 Java基于ssm的空气质量检测系统,检测设备检测一定范围内的企业空气指数,如果有污染则地图显示红色标记. 演示视频 https://www.bilibili.com/video/BV1GK4y ...

  7. 空气质量等级c语言编程,华中科技大学C语言课设 空气质量检测信息管理系统.docx...

    华中科技大学 计算机科学与技术学院PAGE PAGE \* MERGEFORMAT 128 华中科技大学 计算机科学与技术学院 PAGE \* MERGEFORMAT 1 华中科技大学计算机科学与技术 ...

  8. ESP8266-Arduino编程实例-MQ-135空气质量检测传感器驱动

    MQ-135空气质量检测传感器驱动 1.MQ-135介绍 MQ-135气体传感器是一款针对家庭.环境的有害气体检测仪器,适用于氨气.芳香族化合物.硫磺.苯蒸气.烟雾等有害气体检测,气敏元素检测浓度. ...

  9. stm32毕业设计 空气质量检测系统

    文章目录 1 简介 2 系统设计概述 3 系统总体方案 4 硬件设计方案 4.1 stm32 主控 4.2 温度采集模块 4.3 甲醛浓度检测模块 4.4 PM2. 5 浓度检测模块 4.5 液晶显示 ...

最新文章

  1. 一步一步教你如何导出JAR包后将多个JAR包合并,并混淆
  2. Redis的Pub/Sub模式
  3. 请你简要说明一下线程的基本状态以及状态之间的关系?
  4. php查询socket数据包头,php 查询数组值php中关于socket的系列函数总结
  5. BCVP开发者社区2022专属周边第一弹
  6. 一个c#开发的web绘流系统
  7. 常用 ORACLE 函数的解读
  8. ICCV2021 | 最新ICCV2021论文抢先看,附全部下载链接!ICCV2021下载
  9. 二月草的博客开通啦……
  10. debian之快速截图
  11. 没错,我是高端吃瓜玩家(搞懂摩斯密码)
  12. uniapp实现上传图片
  13. 游戏建模:场景模型制作所需软件科普,让建模更简单!
  14. ISIS详细配置实验
  15. 零基础建站教程(二)宝塔面板的使用和CMS的安装
  16. ajax向数据库中添加数据,用jqueryajax在数据库中插入数据
  17. 【Java】RPC与RMI框架
  18. 电脑突然断电蓝屏导致Git错误的一种解决办法
  19. charles证书过期如何处理
  20. 104 货仓选址(绝对值不等式)

热门文章

  1. python turtle笛卡尔心形线_用MATLAB实现心形线
  2. 电商3D展示360全景展示可以突出产品优势
  3. 串联四足机器人基础知识
  4. pm2 for linux
  5. nacos 未读取到合法数据,请检查导入的数据文件
  6. 如何做好创业公司CEO - 量力而行
  7. 云数据库与传统数据库有什么区别
  8. java流意外结束_SyntaxError:输入节点js的意外结束
  9. android的adb使用方法,安卓使用adb教程(无root, 亲测)
  10. 纽约州立计算机科学,SBU的CS「纽约州立大学石溪分校计算机科学专业」