編輯:Android 資訊
繼前面兩篇文章(有問題歡迎反饋):
之後,本文是我對近期開發中應用 NavigationDrawer 特性的詳細總結。文中涉及的所有代碼細節會在文末附上源碼地址,有問題歡迎留言討論。
NavigationDrawer 是 Google 在 Material Design 中推出的一種側滑導航欄設計風格。說起來可能抽象,我們直接看 網易雲音樂 的側滑導航欄效果:

為支持這樣的導航效果,Google 推出了新控件 —— DrawerLayout。在它誕生之前,要實現側滑導航,我們通常會選擇成熟的第三方開源庫(如最有名的 SlidingMenu),效果普遍像 手 Q 那樣:

對比 DrawerLayout 與 SlidingMenu 的實現效果後,基於以下幾點,我認為完全可以用 DrawerLayout 取代以往的 SlidingMenu:
一般情況下,DrawerLayout 布局下只有兩個子布局:一個 內容布局 和一個 側滑菜單布局,關鍵在於 android:layout_gravity 屬性的設置。若想把某個子布局設為左側滑菜單,只需設置 android:layout_gravity="start"(也可以是 left;右側滑則為 end 或 right),未設置的自然成為 內容布局。到底有多簡單?先看下面的布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.DrawerLayout android:id="@+id/simple_navigation_drawer" android:layout_width="match_parent" android:layout_height="match_parent"> <!--內容視圖--> <include android:id="@+id/tv_content" layout="@layout/drawer_content_layout" android:layout_width="match_parent" android:layout_height="match_parent" /> <!--左側滑菜單欄--> <include layout="@layout/drawer_menu_layout" android:layout_width="250dp" android:layout_height="match_parent" android:layout_gravity="start" /> <!--右側滑菜單欄--> <include layout="@layout/drawer_menu_layout" android:layout_width="250dp" android:layout_height="match_parent" android:layout_gravity="end" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout>
到此,你在 Activity 裡什麼都不用做,就已實現下面的側滑效果,簡單到讓人害怕:

在享受便利的同時,Google 也提供了 DrawerLayout 的許多常用 API:打開/關閉側滑欄、控制方向、設置滑動漸變陰影顏色、監聽滑動事件等。

具體代碼請參考工程中的 SimpleDrawerActivity,此處不再貼出。還有一處小細節溫馨提醒:有一次我手誤把 DrawerLayout 的 android:layout_width 設成了 wrap_content,結果出現了下面的異常:

遇到相同情況的同學,把 android:layout_width 改回 match_parent 即可。
在官方 NavigationDrawer 設計中,NavigationView 與 DrawerLayout 是推薦的最佳組合。使用前需先在 build.gradle 中引入(它在 Material Design 兼容包中):
compile 'com.android.support:design:23.1.1'
由於我工程配置的 compileSdkVersion 是 23,所以需要引入 com.android.support:design:23.x.x 版本。要吐槽的是,若引入了 23.1.0,運行後 NavigationView 會報一個 android.view.InflateException 錯誤(又是一個大坑)。
下面簡單介紹 NavigationView 的使用,看幾個相關布局文件:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="NavigationDrawerContent" /> </LinearLayout> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/navigation_drawer_header" app:menu="@menu/navigation_drawer_menu" /> </android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="250dp" android:background="@color/color_512da8"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_margin="10dp" android:text="HeaderLayout" android:textColor="@android:color/white" android:textSize="18sp" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="https://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/item_green" android:icon="@mipmap/green" android:title="Green" /> <item android:id="@+id/item_blue" android:icon="@mipmap/blue" android:title="Blue" /> <item android:id="@+id/item_pink" android:icon="@mipmap/pink" android:title="Pink" /> </group> <item android:title="SubItems"> <menu> <item android:id="@+id/subitem_01" android:icon="@mipmap/ic_launcher" android:title="SubItem01" /> </menu> </item> </menu>
最終得到下面的效果:

總的來說,NavigationView 的關鍵屬性只有 app:headerLayout 和 app:menu,分別對應效果圖頂部的 紫色區域 和下方的 填充菜單項,用法和 DrawerLayout 一樣簡單。
說到 NavigationView,個人認為它設計並不實用,甚至有些呆板。最直接的一點是它的菜單圖標默認會被染成統一的顏色:

第一次運行時,我五顏六色的圖標竟變成這副模樣,差點一口水噴在屏幕上。好在可以調用下面的 API 還原:
mNavigationView.setItemIconTintList(null);//設置菜單圖標恢復本來的顏色
其次,菜單圖標大小與文字間距基本已被 Google 寫死,留給我們改動的空間不多,想調整布局寬高幾乎不可能。所以我至今沒見過國內哪個 App 直接用 NavigationView 做導航(若有歡迎告知)。以上僅為個人看法,歡迎留言討論。
先看網易雲音樂的效果:

主要就是一個線性布局菜單,並結合了 Translucent System Bar 特性(不清楚的同學可看我前面的文章)。大致的布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_cd3e3a"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="65dp" android:background="@color/color_cd3e3a" android:gravity="center" android:text="網易雲音樂" android:textColor="@android:color/white" android:textSize="18sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical"> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/navigation_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/white" android:fitsSystemWindows="true" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:scaleType="centerCrop" android:src="@mipmap/topinfo_ban_bg" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:src="@mipmap/topmenu_icn_msg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的消息" android:textColor="@android:color/black" android:textSize="15sp" /> </LinearLayout> </LinearLayout> </android.support.v4.widget.DrawerLayout>
最終即可實現類似網易雲音樂的效果:


常看到一些 App 左上角有帶感的導航動畫,以前要引入第三方開源庫(如 LDrawer、android-ui)。而現在 Google 推出的 ActionBarDrawerToggle 就能實現,具體可查看我在 NavigationDrawerAnimationActivity 中的實現:

若對這效果不滿意,也可考慮 material-menu 的另一種實現:

前面提到 NavigationView 的不實用性。若你既想要那樣的效果,又渴望更高自由度,功能強大的開源庫 MaterialDrawer 是個不錯的選擇:


到此,NavigationDrawer 的實踐總結基本結束。整體感覺是:自從 Material Design 推出後,Google 的這些新控件越來越簡單易用,讓我們能把精力更多地放在業務代碼上。很多以前要靠第三方庫才能實現的效果,如今已慢慢不需要了。當然,我們依舊可以深入研究這些優秀開源代碼,沉澱更多干貨——這樣,小菜也能慢慢成為大牛。
分享即美德,源碼請看:https://github.com/D-clock/AndroidSystemUiTraining,本篇主要實現代碼如圖中紅圈所示。

封裝一個可收縮的 ListView:長列表不再一次鋪到底
列表裡的資料一多就很尷尬:全部攤開,使用者要滑很久才看到底部;直接分頁,又會打斷閱讀節奏。一個常見又實用的折中做法是「預設顯示兩三條,點『更多』展開全部,再點『收起』復原
Android View 事件傳遞機制:四種場景實驗驗證
Touch 事件分發概覽 學 Android 一年多,今天想以自己的理解講講開發中常用的基礎知識。第一個主題就是 View 的事件傳遞機制。建議先讀一遍 codeKK 中
Android WebView 實現離線緩存閱讀
前言 離線閱讀是許多內容型應用的常見需求。所謂離線下載,並不是指在沒有網路時下載,而是在有網路時先把資源存到本機;離線閱讀則是在網路不佳或沒有網路時,直接讀取本機快取內容
談談 Android 中的 Divider
在 Android 開發中,divider(兩個 View 之間的分割線)幾乎無處不在。最近工作中留意到它並做了些分析,竟發現內藏乾坤,頗有收穫。 ListView 的