构建自己的天气APP(一)

  学习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"

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

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,340评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,762评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,329评论 0 329
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,678评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,583评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,995评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,493评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,145评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,293评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,250评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,267评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,973评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,556评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,648评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,873评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,257评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,809评论 2 339

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,255评论 25 707
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,682评论 22 664
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,975评论 4 60
  • 张清的日精进第18天 体验入 改变从前台接待开始,规范表情,语言,动作,流程。开始都觉得有信心,做起来才发现不习惯...
    kiyoi2017阅读 139评论 0 4
  • →_→ 前几天第一次听南方妹子很调皮的说出:“你乱讲!”这三个字,觉得萌炸了……听了“矮马你跟我扯犊子呢?”这么多...
    刘小胜阅读 555评论 1 4