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 其實是分兩個部分的,一個是頭部,一個是下面的菜單列表部分
如下圖所示:

其中頭部通過 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>
下面的菜單列表部分是一個 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. 菜單列表的點擊事件
菜單列表的點擊事件設(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 的基本使用就差不多搞定了,效果就是前面圖片顯示的效果。
以下是使用過程中遇到的問題及解決方式。
一、菜單圖標顏色被渲染成其他顏色
NavigationView默認會按照 Android 設(shè)計規(guī)范,將菜單里的圖標渲染成itemIconTint所設(shè)置的顏色。如果你沒有設(shè)置這個屬性,則會渲染成它默認的深灰色。如果不想圖標顏色被渲染,可通過以下代碼解決:
navigationView.setItemIconTintList(null);
二、菜單圖標與文字的間距過大
NavigationView的菜單中,圖標與文字的間距為32dp,但是通常與我們的設(shè)計師出的效果不同,這時可以通過重寫以下屬性來進行設(shè)置:
<dimen name="design_navigation_icon_padding" tools:override="true">16dp</dimen>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位Android開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。
- Android抽屜布局DrawerLayout的簡單使用
- Android實現(xiàn)側(cè)滑菜單DrawerLayout
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android布局控件DrawerLayout實現(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-08
Android MessageQueue消息隊列主要作用詳解
Android 消息機制主要指的是 Handler 的運行機制及其所依賴的 MessageQueue 和 Looper 的工作過程,Handler、MessageQueue、Looper組成一個相互聯(lián)系的整體。本文先從 MessageQueue 的源碼來說明其實現(xiàn)原理2023-02-02
Android開發(fā)中ImageView的scaletype屬性用法分析
這篇文章主要介紹了Android開發(fā)中ImageView的scaletype屬性用法,分析了scaletype屬性參數(shù)的常見功能并結(jié)合實例形式給出了具體的使用方法,需要的朋友可以參考下2016-08-08
詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝
這篇文章主要介紹了詳解Android開發(fā)技巧之PagerAdapter實現(xiàn)類的封裝,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android RecyclerView下拉刷新和上拉加載更多
這篇文章主要為大家詳細介紹了Android RecyclerView下拉刷新和上拉加載更多,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
Android應(yīng)用程序簽名步驟及相關(guān)知識介紹
本文主要介紹Android應(yīng)用程序簽名相關(guān)的理論知識,包括:什么是簽名、為什么要給應(yīng)用程序簽名、如何給應(yīng)用程序簽名等,感興趣的朋友可以參考下哈2013-04-04
Android開發(fā)之設(shè)置開機自動啟動的幾種方法
這篇文章主要介紹了Android開發(fā)之設(shè)置開機自動啟動的幾種方法的相關(guān)資料,這里提供三種方法幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08

