Navigation View
抽屜導航是app識别度與内部導航的關鍵,保持這裡設計上的一緻性對app的可用性至關重要,尤其是對于第一次使用的使用者。 NavigationView 通過提供抽屜導航所需的架構讓實作更簡單,同時它還能夠直接通過菜單資源檔案直接生成導航元素。
把NavigationView作為DrawerLayout的内容視圖來使用,比如下面的布局:
<android.support.v4.widget.DrawerLayout 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:fitsSystemWindows="true"> <!-- your content layout --> <android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/drawer_header" app:menu="@menu/drawer"/> </android.support.v4.widget.DrawerLayout> |
你會注意到NavigationView的兩個屬性:app:headerLayout - 控制頭部的布局, app:menu - 導航菜單的資源檔案(也可以在運作時配置)。NavigationView處理好了和狀态欄的關系,可以確定NavigationView
在API21+裝置上正确的和狀态欄互動。最簡單的抽屜菜單就是幾個可點選的菜單集合:
1 2 3 4 5 6 7 8 9 10 11 | |
被點選過的item會高亮顯示在抽屜菜單中,讓使用者知道目前是哪個菜單被選中。
你也可以在menu中使用subheader來為菜單分組:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
你可以通過設定一個OnNavigationItemSelectedListener,使用其setNavigationItemSelectedListener()來獲得元素被選中的回調事件。它為你提供被點選的 菜單元素 ,讓你可以處理選擇事件,改變複選框狀态,加載新内容,關閉導航菜單,以及其他任何你想做的操作。