天气预报案例代码:
1 资源文件
1 资源文件 2 图片
各布局文件代码如下:
(1)activity_main.xml
<?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"
tools:context="zhaohuanhuan.cn.weather.MainActivity"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="347dp"
android:background="#9d9dd7">
<TextView
android:id="@+id/tv_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:text="淄博"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_city"
android:layout_alignBottom="@+id/tv_city"
android:layout_marginLeft="10dp"
android:text="中国"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_currentdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_city"
android:layout_below="@+id/tv_city"
android:layout_marginTop="5dp"
android:text="4月7日,星期五" />
<ImageView
android:id="@+id/iv_condflag"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/tv_city"
android:layout_below="@+id/tv_currentdate"
android:layout_marginTop="5dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_currenttemp"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignTop="@+id/iv_condflag"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/iv_condflag"
android:gravity="center"
android:text="20oC"
android:textSize="30sp" />
<ImageView
android:id="@+id/iv_up"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignTop="@+id/iv_condflag"
android:layout_marginLeft="7dp"
android:layout_toRightOf="@+id/tv_currenttemp"
android:src="@drawable/arrow_up" />
<TextView
android:id="@+id/tv_hightemp"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_alignTop="@+id/iv_condflag"
android:layout_marginLeft="7dp"
android:layout_toRightOf="@+id/iv_up"
android:gravity="center"
android:text="25oC" />
<ImageView
android:id="@+id/iv_down"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignBottom="@+id/iv_condflag"
android:layout_marginLeft="7dp"
android:layout_toRightOf="@+id/tv_currenttemp"
android:src="@drawable/arrow_down" />
<TextView
android:id="@+id/tv_lowtemp"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_alignBottom="@+id/iv_condflag"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/iv_up"
android:gravity="center"
android:text="15oC" />
<TextView
android:id="@+id/tv_currentcond"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_alignLeft="@+id/iv_condflag"
android:layout_below="@+id/iv_condflag"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="多云"
android:textSize="20sp" />
<ImageView
android:id="@+id/iv_hum"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignLeft="@+id/iv_condflag"
android:layout_below="@+id/tv_currentcond"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:src="@drawable/hum" />
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignTop="@+id/iv_hum"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/iv_hum"
android:text="湿度" />
<TextView
android:id="@+id/tv_hum"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignBottom="@+id/iv_hum"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/iv_hum"
android:text="55%" />
<!--第1天天气-->
<TextView
android:id="@+id/tv_firstdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_hum"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="4月7日,星期五" />
<ImageView
android:id="@+id/iv_firstcond"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignLeft="@+id/tv_firstdate"
android:layout_below="@+id/tv_firstdate"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_firsthigh"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="@+id/tv_firstdate"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/iv_firstcond"
android:text="25oC" />
<TextView
android:id="@+id/tv_firstlow"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="@+id/tv_firsthigh"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/iv_firstcond"
android:text="15oC" />
<!--第2天天气-->
<TextView
android:id="@+id/tv_seconddate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_hum"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/tv_firstdate"
android:text="4月8日,星期六" />
<ImageView
android:id="@+id/iv_secondcond"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignLeft="@+id/tv_seconddate"
android:layout_below="@+id/tv_seconddate"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_secondhigh"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout