Android趣味课程:简易天气预报功能

在某些应用程序中,经常有天气预报的功能,通过当地天气情况给予用户合适的建议。比如团购网站根据定位,天气推送用户需要的信息等。本篇博客会使用公共api当中天气预报接口信息,编写一个简易天气预报的界面。

获取天气信息接口如下:

http://api.map.baidu.com/telematics/v3/weather?location=九江&output=json&ak=FkPhtMBK0HTIQNh7gG4cNUttSTyr0nzo

可替换location对应的地址,比如改变location=九江为location=北京,及可以获取北京的天气情况与指数建议。

最终显示界面如下:

      

这里我针对已有接口中提供的信息,简易布局内容,查看上图,包括控件有:Toolbar,ListView,Spinner以及一些简单组件。接下来,我们就根据这个布局完成相关功能:

首先完成activity对应的布局界面编写:activity_forecast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:title="天气预报"
        app:titleTextColor="#FFF"
        app:subtitle="九江"
        app:subtitleTextColor="#FFF"
        app:logo="@mipmap/ic_launcher"></android.support.v7.widget.Toolbar>
    <TextView
        android:text="近4天的天气情况:"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Spinner
        android:id="@+id/forecast_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </Spinner>
    <TextView
        android:text="今日指数信息:"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ListView
        android:id="@+id/forecast_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/colorPrimary"
        android:dividerHeight="1dp">
    </ListView>
</LinearLayout>

观察发现,这里涉及到listview和spinner,所以需要编写每一个item的布局,我先编写listview对应的布局:item_forecast_lv.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">
    <TextView
        android:id="@+id/item_lv_tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="洗车"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/item_lv_tv_zs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="舒适"/>
    <TextView
        android:id="@+id/item_lv_tv_tipt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="洗车指数"/>
    <TextView
        android:id="@+id/item_lv_tv_des"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#eee"
        android:padding="10dp"
        android:text="较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"/>
</LinearLayout>

接下来编写spinner下拉列表对应的item的布局内容,在下拉列表的item中主要用于显示温度,风向等信息。item_forecast_sp.xml内容为:


                
  • 9
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值