Android中NavigationView的使用與相關(guān)問題解決
一、基本使用
1. NavigationView 在 design 庫中,添加依賴(最新的是 23.2.0);
compile 'com.android.support:design:23.1.1'
2. 然后在 DrawerLayout 布局中添加 NavigationView ;
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" 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"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> ...... </LinearLayout> </FrameLayout> <android.support.design.widget.NavigationView android:id="@+id/navigation" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header" app:menu="@menu/activity_main_drawer"/> </android.support.v4.widget.DrawerLayout>
其中需要注意給 NavigationView 設(shè)置 android:layout_gravity="start"
屬性。
3.然后注意到 NavigationView 其實(shí)是分兩個(gè)部分的,一個(gè)是頭部,一個(gè)是下面的菜單列表部分
如下圖所示:
其中頭部通過 app:headerLayout="@layout/nav_header"
屬性添加,nav_header 的布局如下:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="192dp" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/nav_header_bg" android:scaleType="centerCrop"/> <ImageView android:layout_width="96dp" android:layout_height="96dp" android:layout_gravity="bottom" android:layout_marginBottom="36dp" android:padding="8dp" android:src="@drawable/ic_avatar"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:padding="16dp" android:text="Jaeger" android:textAppearance="@style/TextAppearance.AppCompat.Body1"/> </FrameLayout>
下面的菜單列表部分是一個(gè) menu 文件,通過 app:menu="@menu/activity_main_drawer"
屬性添加。
activity_main_drawer.xml 文件在 menu 文件夾下,內(nèi)容為:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_camera" android:icon="@drawable/ic_menu_camera" android:title="Import"/> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_menu_gallery" android:title="Gallery"/> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_menu_slideshow" android:title="Slideshow"/> <item android:id="@+id/nav_manage" android:icon="@drawable/ic_menu_manage" android:title="Tools"/> </group> <item android:title="Communicate"> <menu> <item android:id="@+id/nav_share" android:icon="@drawable/ic_menu_share" android:title="Share"/> <item android:id="@+id/nav_send" android:icon="@drawable/ic_menu_send" android:title="Send"/> </menu> </item> </menu>
4. 菜單列表的點(diǎn)擊事件
菜單列表的點(diǎn)擊事件設(shè)置代碼如下:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.nav_personal_info: // do something break; ... } return false; } });
至此,NavigationView 的基本使用就差不多搞定了,效果就是前面圖片顯示的效果。
以下是使用過程中遇到的問題及解決方式。
一、菜單圖標(biāo)顏色被渲染成其他顏色
NavigationView默認(rèn)會(huì)按照 Android 設(shè)計(jì)規(guī)范,將菜單里的圖標(biāo)渲染成itemIconTint
所設(shè)置的顏色。如果你沒有設(shè)置這個(gè)屬性,則會(huì)渲染成它默認(rèn)的深灰色。如果不想圖標(biāo)顏色被渲染,可通過以下代碼解決:
navigationView.setItemIconTintList(null);
二、菜單圖標(biāo)與文字的間距過大
NavigationView的菜單中,圖標(biāo)與文字的間距為32dp,但是通常與我們的設(shè)計(jì)師出的效果不同,這時(shí)可以通過重寫以下屬性來進(jìn)行設(shè)置:
<dimen name="design_navigation_icon_padding" tools:override="true">16dp</dimen>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。
- Android抽屜布局DrawerLayout的簡(jiǎn)單使用
- Android實(shí)現(xiàn)側(cè)滑菜單DrawerLayout
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android布局控件DrawerLayout實(shí)現(xiàn)完美側(cè)滑效果
- Android使用DrawerLayout仿QQ6.0雙側(cè)滑菜單
- Android關(guān)于BottomNavigationView使用指南
- Android NavigationView頭部設(shè)置監(jiān)聽事件
- Android BottomNavigationView底部導(dǎo)航效果
- Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用
相關(guān)文章
微信支付僅能成功調(diào)用一次問題的解決方法(Android)
這篇文章主要介紹了微信支付僅能成功調(diào)用一次問題的解決方法,感興趣的小伙伴們可以參考一下2016-08-08Android實(shí)現(xiàn)定時(shí)任務(wù)及鬧鐘
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)任務(wù)及鬧鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Android MessageQueue消息隊(duì)列主要作用詳解
Android 消息機(jī)制主要指的是 Handler 的運(yùn)行機(jī)制及其所依賴的 MessageQueue 和 Looper 的工作過程,Handler、MessageQueue、Looper組成一個(gè)相互聯(lián)系的整體。本文先從 MessageQueue 的源碼來說明其實(shí)現(xiàn)原理2023-02-02教你一步步實(shí)現(xiàn)Android微信自動(dòng)搶紅包
自從微信添加搶紅包的功能,微信的電商之旅算是正式開始正式火爆起來。但是作為Android開發(fā)者來說,我們首先考慮的是如何實(shí)現(xiàn)Android微信自動(dòng)搶紅包呢,下面我們來一起看看吧。2016-08-08Android開發(fā)中ImageView的scaletype屬性用法分析
這篇文章主要介紹了Android開發(fā)中ImageView的scaletype屬性用法,分析了scaletype屬性參數(shù)的常見功能并結(jié)合實(shí)例形式給出了具體的使用方法,需要的朋友可以參考下2016-08-08詳解Android開發(fā)技巧之PagerAdapter實(shí)現(xiàn)類的封裝
這篇文章主要介紹了詳解Android開發(fā)技巧之PagerAdapter實(shí)現(xiàn)類的封裝,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Android RecyclerView下拉刷新和上拉加載更多
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView下拉刷新和上拉加載更多,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android應(yīng)用程序簽名步驟及相關(guān)知識(shí)介紹
本文主要介紹Android應(yīng)用程序簽名相關(guān)的理論知識(shí),包括:什么是簽名、為什么要給應(yīng)用程序簽名、如何給應(yīng)用程序簽名等,感興趣的朋友可以參考下哈2013-04-04Android開發(fā)之設(shè)置開機(jī)自動(dòng)啟動(dòng)的幾種方法
這篇文章主要介紹了Android開發(fā)之設(shè)置開機(jī)自動(dòng)啟動(dòng)的幾種方法的相關(guān)資料,這里提供三種方法幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08