当前位置: 首页 > news >正文

kotlin 编写一个简单的天气预报app(四)增加界面显示

编写界面来显示返回的数据

用户友好性:通过界面设计和用户体验优化,可以使天气信息更易读、易理解和易操作。有效的界面设计可以提高用户满意度并提供更好的交互体验。

增加城市名字的TextView

    <TextViewandroid:id="@+id/textViewCityName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="200dp"android:height="50dp"app:layout_constraintTop_toBottomOf="@+id/buttonSearch"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"android:textSize="20sp"android:gravity="center"/>
  • android:id="@+id/textViewCityName":给 TextView 设置了一个唯一的标识符,以便于在代码中对其进行引用。
  • android:layout_width="wrap_content" android:layout_height="wrap_content":设置 TextView 的宽度和高度分别根据文本内容自动调整。
  • android:width="200dp" android:height="50dp":指定 TextView 的宽度和高度为 200dp 和 50dp。
  • app:layout_constraintTop_toBottomOf="@+id/buttonSearch":将 TextView 的顶部与 id 为 buttonSearch 的视图的底部对齐。
  • app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent":将 TextView 的左侧与父容器的左侧对齐,将 TextView 的右侧与父容器的右侧对齐。
  • android:textSize="20sp":设置 TextView 的文本字体大小为 20sp。
  • android:gravity="center":将文本内容在 TextView 中水平和垂直居中。

增加温度显示部分

    <TextViewandroid:id="@+id/textViewTemperature"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintTop_toBottomOf="@+id/textViewCityName"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"android:textSize="30sp"android:gravity="center"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintTop_toTopOf="@+id/textViewTemperature"app:layout_constraintStart_toEndOf="@id/textViewTemperature"android:text="@string/degree"android:textSize="20sp"android:gravity="center" /><TextViewandroid:id="@+id/textViewMaxMinTemperature"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintTop_toBottomOf="@id/textViewTemperature"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"android:textSize="12sp"android:gravity="center" />

增加天气内容

    <TextViewandroid:id="@+id/textViewWeather"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintTop_toBottomOf="@id/textViewMaxMinTemperature"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent"android:textSize="20sp"android:gravity="center" />

编写逻辑填充数据

    @SuppressLint("SetTextI18n")@Subscribe(threadMode = ThreadMode.MAIN)fun onEvent(event: WeatherResponseEvent) {val weatherResponse = event.weatherResponseval kelvins = 273.15val cityName = weatherResponse.nameval temperature = weatherResponse.main?.temp?.minus(kelvins)val maxTemperature = weatherResponse.main?.temp_max?.minus(kelvins)val minTemperature = weatherResponse.main?.temp_min?.minus(kelvins)findViewById<TextView>(R.id.textViewCityName).text = cityNamefindViewById<TextView>(R.id.textViewTemperature).text = temperature?.toInt().toString()findViewById<TextView>(R.id.textViewMaxMinTemperature).text = "${maxTemperature?.toInt()} / ${minTemperature?.toInt()}"findViewById<TextView>(R.id.textViewWeather).text = "${weatherResponse.weather.first().main} | ${weatherResponse.weather.first().description}"}

最后的效果:

在这里插入图片描述

http://www.lryc.cn/news/105621.html

相关文章:

  • 英语不好能学好Python吗?Python常用英文单词汇总
  • Counting Stars 2023“钉耙编程”中国大学生算法设计超级联赛(5)hdu7335
  • 浅谈document.write()输出样式
  • AIGC(Artificial Intelligence and Graph Computing)职业发展路径和前景如何?
  • MySql006——基本的SELECT查询语句
  • 【啥都生】分类项目中的模型搭建代码解析
  • Ubuntu出现了内部错误
  • Stable Diffusion AI绘画初学者指南【概述、云端环境搭建】
  • 小程序动态隐藏分享按钮
  • 语音合成是什么?如何进行语音合成TTS数据采集?
  • 实用干货!一文读懂Salesforce中6种数据关系类型!
  • Spring引入外部数据源
  • word里的页码问题
  • ​LeetCode解法汇总142. 环形链表 II
  • 危化品行业防雷检测综合解决方案
  • 刷题笔记:day 1
  • Linux——平台设备及其驱动
  • 【C语言技巧】三种多组输入的写法
  • DB2数据库巡检脚本
  • Eureka 学习笔记3:EurekaHttpClient
  • Android Framework 之 启动流程
  • Qt、C/C++环境中内嵌LUA脚本、实现LUA函数的调用执行
  • 超详细 | 模拟退火算法及其MATLAB实现
  • 在线餐饮油烟实时监测系统的设计与实现
  • 7-2 凯撒密码 (20分)
  • LeetCode_贪心算法_中等_763.划分字母区间
  • 【算法提高:动态规划】1.5 状态压缩DP TODO
  • 建网站一般使用Windows还是liunx好?
  • NodeJs后端项目使用docker打包部署
  • ARM单片机中断处理过程解析