最近在学习Android,这里就用这个极简天气app练练手吧。
虽然简陋,但是我们将学习以下内容:
- Android单线程模型
- 两个Activity之间传递数据
- 异步类AsyncTask
- 数据结构Map
- Json格式解析
- 多级联动的滚轮
制作思路:
- UI布局
- 城市选择
- 对应城市天气数据获取与处理
- 刷新UI
先看看效果吧(前面有点迟钝):
我点击右上角的城市管理按钮后进入城市选择(里面的城市是随便写的哦),确认后返回主界面,这里只显示高温而已。
确实极简吧,但是其中的处理过程可是一点没有少的!是要少加布局上的修饰就瞬间高大上了。
先看布局文件(activity_main.xml):
@+id/activity_main_textview_city这是用于显示我们选择的城市的TextView。
@+id/activity_main_textview_content这是显示天气内容的TextView(我偷懒把所有的内容都放进去了,嘿嘿,嘿嘿)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/activity_main_button_chengshiguanli"
android:layout_width="50dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:text="@string/chengshiguanli"
android:background="@color/colorWhite"
android:textSize="5pt"/>
<TextView
android:id="@+id/activity_main_textview_city"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="17pt"
android:gravity="center"
/>
<TextView
android:id="@+id/activity_main_textview_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/activity_main_textview_city"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
/>
</RelativeLayout>
下面是选择城市的布局文件(choose_city.xml):
其中我们用到了com.wx.wheelview.widget.WheelView组件,这是一个开源的滚轮组件,使用之前需要先导入相应的包,这里是其项目地址和详细的使用说明:https://github.com/venshine/WheelView
"@+id/city_btn",android:text="boom" 这个按钮是我们做测试用的。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.wx.wheelview.widget.WheelView
android:id="@+id/wheel_province"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<com.wx.wheelview.widget.WheelView
android:id="@+id/wheel_city"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<com.wx.wheelview.widget.WheelVi