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

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

首先新建一个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">
    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:background="#00ff00">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="空气温度"
            android:textSize="25sp"/>
        <TextView
            android: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>

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:background="#00ff00">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="空气湿度"
            android:textSize="25sp"/>
        <TextView
            android: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>

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:background="#00ff00">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="光照"
            android:textSize="25sp"/>
        <TextView
            android: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>

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:background="#00ff00">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="CO2"
            android:textSize="25sp"/>
        <TextView
            android: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>

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:background="#00ff00">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="PM2.5"
            android:textSize="25sp"/>
        <TextView
            android: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>

    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:background="#00ff00">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="道路状况"
            android:textSize="25sp" />

        <TextView
            android: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">
    <RelativeLayout
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:background="#00ff00">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:text="空气温度"
            android:textSize="25sp"/>
        <TextView
            android: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博客

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

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

 是不是很简单?

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

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
某城市已经在各条道路上安装了空气温度、空气湿度、pm2.5、CO2 、光照、道路状态等传感器。部分小车安装了ETC和速度传感器,能够获得这些小车的数度和对其ETC金额进行管理。各传感数据已经汇总在服务器系统。 假设各传感器和ETC账户最小、最大阈值已由管理员设置如下: 环境指标 最小值 最大值 备注 空气温度: 10 40 空气湿度: 50 150 pm2.5 500 5000 CO2 100 600 光照 0 100 道路状态: 1 5 ETC账户余额 100 5000 现要求开发一套移动APP实现如下功能: 1、用户登录注册模块的功能 对用户账号的合法性进行判断,合法的用户允许使用智能交通系统,不合法的用户则禁止使用该系统。用户登陆注册模块能够完成用户注册、自动登录和找回密码等功能。 2、实现系统的实时环境指标动态显示功能 图1 界面原型 1)、利用给定的资源,实现该界面原型的布局,参阅环境指标界面原型图。 2)、实现空气温度、空气湿度、pm2.5、CO2 、光照、道路状态(默认1号编号道路)实时数据显示功能。 注:数据实时刷新周期为 5秒。 3)、实现报警状态警示功能,正常状态背景为绿色,警告状态为红色。 4)、点击传感器的显示区域,可以进入对应的传感器“实时曲线显示”界面。 3 实现系统车辆账户充值、查询功能和限速功能 1)、在点击充值按钮时,先检测账户余额是否超过设置的阈值,如果超过阈值就不允许充值。 2)、如果用户充值的金额加上账户余额超过了账户余额的最大阈值就提示用户充值失败,并提示出本次可以充值的最大额度。 3)、设置小车速度阈值并且显示到页面。 4)、实时监测小车的速度一旦小车速度低于小车最低速度阈值,提示用户速度过慢。一旦小车速度超过最大速度阈值强制停止小车。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程学渣ズ

谢谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值