Android基础——天气预报(网络接口获取数据,XML解析)

本文介绍了如何在Android应用中实现天气预报功能,包括添加必要的权限、设计UI布局,如TabLayout和ViewPager,以及如何处理XML数据解析。通过网络接口获取数据并展示在ListView和Spinner中,详细讲解了各个布局和组件的设置。
摘要由CSDN通过智能技术生成

案例要求:

首先加权限:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

先布局,由要求可得五个布局

主界面包含一个TabLayout和一个ViewPager,TabLyout需要导依赖:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.animee.test.MainActivity">
    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabIndicatorHeight="5dp">

    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

</LinearLayout>
第二个布局,Fragment布局,包含一个ListView

<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"
    android:orientation="vertical"
    tools:context="com.animee.test.WeatherFragment">

    <!-- TODO: Update blank fragment layout -->
    <ListView
        android:id="@+id/fragment_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/colorPrimary"
        android:dividerHeight="1dp" />

</LinearLayout>

第三个布局,Fragment中ListView的头部布局,包含两个标识的TextView和一个Spinner:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/header_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="近期天气情况:"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"/>

    <Spinner
        android:id="@+id/header_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/header_tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="今日指数分析:"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"/>
</LinearLayout>
第四个布局,ListView中的布局

<?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"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/lv_tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="穿衣"
        android:textSize="20sp"
        android:textColor="@color/colorAccent"/>

    <TextView
        android:id="@+id/lv_tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/lv_tv1"
        android:layout_marginTop="10dp"
        android:text="穿衣指数" />

    <TextView
        android:id="@+id/lv_tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/lv_tv2"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_toEndOf="@+id/lv_tv2"
        android:layout_toRightOf="@+id/lv_tv2"
        android:text="冷" />

    <TextView
        android:id=
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值