Material Design 組件之NavigationView

今天來看一下 NavigationView 的使用,NavigationView 是一個標(biāo)準(zhǔn)的導(dǎo)航菜單,其菜單內(nèi)容由菜單資源文件來填充,NavigationView 一般和 DrawerLayout 一起搭配使用構(gòu)成抽屜菜單,分別由內(nèi)容頁和菜單頁組成。

基本布局

可以直接使用 DrawerLayout 作為根布局,里面依次是內(nèi)容布局和菜單布局,切記內(nèi)容布局一定是在菜單布局的前面,可以這樣理解菜單劃出的時候肯定應(yīng)該在內(nèi)容布局之上,如果兩者順序相反,則影響菜單 Item 的點擊事件以及菜單的滑動隱藏,當(dāng)然如果有 ToolBar 等,可以按需添加到內(nèi)容布局中,也可以 DrawerLayout 外,唯一區(qū)別是側(cè)換菜單是否遮擋 ToolBar,基本使用參考如下:

 <android.support.v4.widget.DrawerLayout  
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/drawer_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fitsSystemWindows="true">
     
     <!--內(nèi)容 -->
     
     <!--菜單-->
     <android.support.design.widget.NavigationView
         android:id="@+id/navigation"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_gravity="start"
         app:menu="@menu/my_navigation_items" />
 </android.support.v4.widget.DrawerLayout>

常用屬性

下面是 NavigationView 的常用屬性,具體如下:

<!--菜單彈出方向-->
android:layout_gravity="start"
<!--菜單圖標(biāo)渲染的顏色-->
app:itemIconTint="@color/colorPrimary"
<!--菜單文字的顏色-->
app:itemTextColor="@color/colorNormal"
<!--菜單項背景顏色(組之間有間隔)-->
app:itemBackground="@color/colorBackground"
<!--菜單項-->
app:menu="@menu/menu_navigation_view" 
<!--NavigationView的頭部布局-->
app:headerLayout="@layout/head_navigation_layout"

文字選中效果

如果美工比較用心會告訴點擊時是那種顏色、按下是那種顏色或者是某種效果,此時就需要設(shè)置菜單項文字選中效果了,這里選擇創(chuàng)建在 color 資源文件的形式來實現(xiàn)文字選中效果了,定義 color 資源文件如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--按下-->
    <item android:color="@color/colorPress" android:state_pressed="true"/>
    <!--選中-->
    <item android:color="@color/colorCheck" android:state_checked="true"/>
    <!--默認(rèn)-->
    <item android:color="@color/colorNormal"/>
</selector>

然后,設(shè)置 NavigationView 的 itemTextColor 屬性即可,具體如下:

<!--設(shè)置菜單項顏色-->
app:itemTextColor="@color/select_color_navigation"

當(dāng)然,也可以在代碼中設(shè)置,具體如下:

//設(shè)置菜單項顏色
ColorStateList colorStateList = getResources().getColorStateList(R.color.select_color_navigation);
navigationView.setItemTextColor(colorStateList);

然后,設(shè)置對 NavigationView 菜單項選中的事件監(jiān)聽,具體如下:

navigationView.setNavigationItemSelectedListener(this);

最后,在點擊完成要設(shè)置該菜單項已被選中,具體如下:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    switch (item.getItemId()){
        case R.id.menu1:
            Toast.makeText(this,"menu1",Toast.LENGTH_SHORT).show();
            break;
        //...
    }
    //設(shè)置菜單項選中
    item.setCheckable(true);
    //關(guān)閉Drawer
//    navDrawLayout.closeDrawers();
    return true;
}

圖標(biāo)與文字間距

使用 NavigationView 之后發(fā)現(xiàn),菜單圖標(biāo)與菜單文字之間有一定的間距,看著有點寬,有一點強(qiáng)迫癥必須稍微修改小一點,根據(jù)之前修改 FloatingActionButton 大小的經(jīng)驗,翻一下源碼找設(shè)置這個間距的位置,查看 NavigationView 源碼,最終在 NavigationMenuPresenter 中找到相關(guān)的 dimen 值,然后順藤摸瓜找到與 Navigation 相關(guān)的幾個 dimen 值,具體如下:

public static final int design_navigation_elevation = 0x7f060064;
public static final int design_navigation_icon_padding = 0x7f060065;
public static final int design_navigation_icon_size = 0x7f060066;
public static final int design_navigation_max_width = 0x7f060067;
public static final int design_navigation_padding_bottom = 0x7f060068;
public static final int design_navigation_separator_vertical_padding = 0x7f060069;

此時,在項目的 dimens 文件夾中創(chuàng)建名稱相同的值覆蓋即可,這里是修改 Menu 圖標(biāo)與文字之間的間距,所以我們只要設(shè)置:

<!--修改NavigationView菜單圖標(biāo)與文字之間的間距-->
<dimen name="design_navigation_icon_padding" tools:override="true">10dp</dimen>

至于其他相關(guān)的 dimen 值就不一一說明了,這樣就修改了 Menu 圖標(biāo)與文字之間的間距。

案例

下面是一個 NavigationView 結(jié)合 DrawerLayout 的使用案例,布局如下:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--ToolBar-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/navToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/navDrawLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--內(nèi)容-->
        <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="@string/app_name"
                android:textSize="18sp" />
        </LinearLayout>
        <!--菜單-->
        <android.support.design.widget.NavigationView
            android:id="@+id/navigationView"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/head_navigation_layout"
            app:itemIconTint="@color/colorPrimary"
            app:itemTextColor="@color/select_color_navigation"
            app:menu="@menu/menu_navigation_view" />
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

顯示效果

下面是顯示效果,如下圖所示:

NavigationView
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,578評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,701評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,691評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,974評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,694評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,026評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,015評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,193評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,719評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,442評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,668評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,151評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,846評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,255評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,592評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,394評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內(nèi)容