美文网首页
构建自己的天气APP(一)

构建自己的天气APP(一)

作者: BigTotoro | 来源:发表于2017-10-10 11:42 被阅读0次

  学习android最大的快乐莫过于写自己的APP,并且分享给亲朋好友使用,所以想写这一些列的文章,来记述写一个简单能用的天气APP的过程。

  天气app算是好上手的了,只要你有基础的android知识,就可以做到一边探索,一边开发,最后构建出来。附上我的小作品 龙猫天气

写一个天气的app都要哪些东西?

  • 会写基本的UI布局
  • 熟悉android的语法和组件的使用
  • 正确完善的天气数据
  • 准确的定位

  我们一个一个来看。第一个是android的基础,相信学过android的话,肯定是难不倒你的,最多就是写的不熟练,不要紧,写玩这个app就会熟悉多了。
  第二个组件的使用和语法一般书上都是有的,我自己写的天气app所需的组件在《第一行代码》这本书里都有写到,会查阅就可以了。
  天气数据的来源我推荐和风天气。而定位服务我推荐高德地图。在此再次感谢他们提供的免费数据,非常感谢!!!

构建界面

  以下是我的界面,图源自PonyWeather,是个很赞的开源项目,教会了我很多很多东西。

龙猫天气主界面 侧滑菜单

  大家可以按照自己所学来构建自己的界面。这里介绍下我自己的。
  先上代码

<android.support.v4.widget.DrawerLayout 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:id="@+id/dl_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="totoro.application.xkf.totoroweather.activity.WeatherActivity">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:fitsSystemWindows="true">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/ctl_collapsing_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/iv_header_image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    android:src="@mipmap/header_image_weather" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/tb_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:elevation="4dp"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="pin" />

            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/srl_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <android.support.v7.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginTop="8dp"

                        app:cardCornerRadius="8dp">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="8dp"
                            android:gravity="center_vertical"
                            android:orientation="horizontal">

                            <ImageView
                                android:id="@+id/iv_now_weather_icon"
                                android:layout_width="?attr/actionBarSize"
                                android:layout_height="?attr/actionBarSize"
                                android:scaleType="fitXY"
                                android:src="@mipmap/ic_weather_icon_999" />

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:gravity="end"
                                android:orientation="vertical">

                                <TextView
                                    android:id="@+id/tv_tmp_and_weather"
                                    android:layout_width="wrap_content"
                                    android:layout_height="0dp"
                                    android:layout_weight="3"
                                    android:text="@string/unKnow"
                                    android:textColor="@color/grey"
                                    android:textSize="35sp" />

                                <TextView
                                    android:id="@+id/tv_feel_and_wind"
                                    android:layout_width="wrap_content"
                                    android:layout_height="0dp"
                                    android:layout_weight="1"
                                    android:text="@string/unKnow" />
                            </LinearLayout>
                        </LinearLayout>
                    </android.support.v7.widget.CardView>

                    <android.support.v7.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginTop="8dp"
                        app:cardCornerRadius="8dp">

                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/rv_hourly_list"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:nestedScrollingEnabled="false" />
                    </android.support.v7.widget.CardView>

                    <android.support.v7.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginTop="8dp"
                        app:cardCornerRadius="8dp">

                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/rv_forecast_list"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:nestedScrollingEnabled="false" />
                    </android.support.v7.widget.CardView>

                    <android.support.v7.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginTop="8dp"
                        app:cardCornerRadius="8dp">

                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/rv_suggestion_list"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:nestedScrollingEnabled="false" />
                    </android.support.v7.widget.CardView>


                </LinearLayout>
            </android.support.v4.widget.NestedScrollView>
        </android.support.v4.widget.SwipeRefreshLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_search"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="24dp"
            android:layout_marginEnd="24dp"
            android:elevation="8dp"
            app:srcCompat="@android:drawable/ic_menu_search" />
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nv_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_head_layout"
        app:menu="@menu/navigation_menu" />
</android.support.v4.widget.DrawerLayout>

   一层一层来说

  • 套在最外面的DrawerLayout就是那个侧滑组件了,它的里面支持两个子布局,一个是正常的界面,一个就是侧滑出来的界面,对应我的代码里面就是CoordinatorLayout和NavigationView这两个组件啦。在activity中find出来,然后调用他的openDrawer(Gravity.START)显示它, 调用closeDrawers();来隐藏它。

  • CoordinatorLayout是一个很强大的布局,它会对布局里面的组件做出一些反应。比如在下面的那个FloatingActionButton,如果你使用snakebar的话,悬浮按钮会随着sanckbar的出现而上升,随着snackbar的消失而下降

  • 再往里一层就是AppBarLayout和CollapsingToolbarLayout了。他们组合在一起可以实现让toolbar缩放的动画效果。看我的主界面,最上面是个图,随着上滑会最终变为一个正常的toolbar,中间的动画效果非常的好

  • 再往后就是SwipeRefreshLayout了,简单来说就是你把组件放到里面就会带有下拉刷新的效果,简单易用。在activity中find出来,之后可以这样

    srlRefreshLayout.setOnRefreshListener(this);
      @Override
    public void onRefresh() {
        //具体的逻辑
    }
  • NestedScrollView可以让子组件变得可滑动。我的板块主要是4个CardView,总长度超过一个屏幕,CardView里面又是有Recycleview的,所以必须套用到这个组件。这里有一个细节要注意,就是Recycleview和
    NestedScrollView都是可滑动的,如果不做任何处理的话,在主界面滑动体验会很差,手指离开屏幕就停了。这里要在RecycleView里面加上这么一句 android:nestedScrollingEnabled="false",意思是判断滑动的时候放弃自己的滑动,交给外部的NestedScrolling。这样一来滑动就变得很自然了。
  • 侧滑菜单的界面很简单,就是一个menu文件和一个imageView,这里没有给出。在NavigationView中加这两行
        app:headerLayout="@layout/navigation_head_layout"
        app:menu="@menu/navigation_menu"

  就会把你的布局放进去,组成侧滑的界面了

  上面的代码在《第一行代码》中大多都是能查到的。就像我上一篇文章说的那样,会查书就足够了,不用再当时都记住它。

  以上就是我的界面分析了,有了主界面,就可以开始下一步行动了,我会在下一篇文章中继续写。如果能帮到你,那真的很荣幸。

相关文章

网友评论

      本文标题:构建自己的天气APP(一)

      本文链接:https://www.haomeiwen.com/subject/wzqvyxtx.html