Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用
現(xiàn)在 Android Studio 已經(jīng)直接提供左滑菜單功能,只需要在創(chuàng)建新項(xiàng)目時(shí)選擇 Navigation Drawer Activity 就可以直接創(chuàng)建一個(gè)有左滑菜單功能的 APP。
DrawerLayout
DrawerLayout 是 support-v4 Library 包中實(shí)現(xiàn)了側(cè)滑菜單效果的控件,可以說 drawerLayout 是因?yàn)榈谌娇丶?MenuDrawer 等的出現(xiàn)之后,Google 借鑒而出現(xiàn)的產(chǎn)物。DrawerLayout 分為側(cè)邊菜單和主內(nèi)容區(qū)兩個(gè)部分,側(cè)邊菜單可以根據(jù)手勢(shì)展開與隱藏(drawerLayout 自身特性),主內(nèi)容區(qū)可以隨著菜單的點(diǎn)擊而變化。
1. 抽屜式導(dǎo)航欄是顯示應(yīng)用主導(dǎo)航菜單的界面面板。當(dāng)用戶觸摸應(yīng)用欄中的抽屜式導(dǎo)航欄圖標(biāo)或從屏幕的左邊緣滑動(dòng)手指時(shí),就會(huì)顯示抽屜式導(dǎo)航欄;
2. 抽屜式導(dǎo)航欄圖標(biāo)會(huì)顯示在使用 DrawerLayout 的所有頂級(jí)目的地上。頂級(jí)目的地是應(yīng)用的根級(jí)目的地。它們不會(huì)在應(yīng)用欄中顯示向上按鈕。
3. 要添加抽屜式導(dǎo)航欄,請(qǐng)先聲明 DrawerLayout 為根視圖。在 DrawerLayout 內(nèi),為主界面內(nèi)容以及包含抽屜式導(dǎo)航欄內(nèi)容的其他視圖添加布局。
4. 例如,以下布局使用含有兩個(gè)子視圖的 DrawerLayout: 包含主要內(nèi)容的 NavHostFragment 和適用于抽屜式導(dǎo)航欄內(nèi)容的 NavigationView。
<?xml version="1.0" encoding="utf-8"?> <!-- Use DrawerLayout as root container for activity --> <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"> <fragment android:name="androidx.navigation.fragment.NavHostFragment" android:id="@+id/nav_host_fragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/mobile_navigation" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" /> </android.support.v4.widget.DrawerLayout>
android:name 指定 NavHost 的實(shí)現(xiàn)類,這里是 NavHostFragment;
app:defaaultNavHost="true" 攔截系統(tǒng)返回鍵,當(dāng)用戶按下返回鍵時(shí),系統(tǒng)會(huì)將正在展示的 Fragment 返回。
app:navGraph 關(guān)聯(lián)導(dǎo)航圖;mobile_navigation.xml 是放在 res --> navigation 文件加里,是一個(gè)包含導(dǎo)航目的地(fragment)的菜單;
mobile_navigation.xml 具體內(nèi)容
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mobile_navigation" app:startDestination="@+id/nav_home"> <fragment android:id="@+id/nav_home" android:name="com.example.leftnavigationbar.ui.home.HomeFragment" android:label="@string/menu_home" tools:layout="@layout/fragment_home" /> <fragment android:id="@+id/nav_gallery" android:name="com.example.leftnavigationbar.ui.gallery.GalleryFragment" android:label="@string/menu_gallery" tools:layout="@layout/fragment_gallery" /> <fragment android:id="@+id/nav_slideshow" android:name="com.example.leftnavigationbar.ui.slideshow.SlideshowFragment" android:label="@string/menu_slideshow" tools:layout="@layout/fragment_slideshow" /> </navigation>
android:id 必填項(xiàng),后面 fragment跳轉(zhuǎn)的時(shí)候要用到;
android:name Fragment 的全路徑;
android:label 要跳轉(zhuǎn)的 Fragment 在菜單里的名字
tools:layout Fragment 的布局;
NavigationView
NavigationView 表示應(yīng)用程序的標(biāo)準(zhǔn)導(dǎo)航菜單,菜單內(nèi)容可以由菜單資源文件填充。NavigationView 通常放在 DrawerLayout 中,可以實(shí)現(xiàn)側(cè)滑效果的 UI 。DrawerLayout 布局可以有3個(gè)子布局,第一個(gè)布局必須是主界面而且不可以不寫,其他2個(gè)布局就是左、右兩個(gè)側(cè)滑布局,左右兩個(gè)側(cè)滑布局可以只寫其中一個(gè)。
android:laoyout_gravity 值為 start 則是從左側(cè)滑出,值為 end 則是從右側(cè)滑出;
android:layout_gravity="start"
android:layout_gravity="end"
app:headerLayout 給 NavigationView 設(shè)置頭文件;
app:headerLayout="@layout/nav_header_main"
nav_header_man.xml 是包含頭部布局樣式的布局文件。
app:menu NavigationView 是通過菜單形式在布局中放置元素的,值為自己創(chuàng)建的菜單文件;
app:menu="@menu/activity_main_drawer"
在 src 下新建一個(gè) menu 文件夾,activity_main_drawer.xml 是放在 menu 下的菜單布局文 件。里面是 Home / Gallery / Slideshow 等 item??梢栽O(shè)置每個(gè) item 的 title、icon等。
app:itemIconTint 設(shè)置菜單圖標(biāo)的顏色;
app:itemTextColor 設(shè)置菜單文字的顏色;
app:itemBackground 設(shè)置菜單背景的顏色;
android:src與app:srcCompat
二者幾乎沒有區(qū)別,都是加載圖片資源。
app:srcCompat將矢量可繪制對(duì)象集成到應(yīng)用程序中。矢量可繪制對(duì)象允許您用XML中定義的單個(gè)矢量圖形替換多個(gè)png資產(chǎn)。從Android支持庫(kù)23.3.0開始,支持向量可繪制對(duì)象只能通過加載app:srcCompat。
app:srcCompat設(shè)置一個(gè)可繪制對(duì)象作為此ImageView的內(nèi)容,它將以其原始大小顯示。
fitsSystemWindows
android:fitsSystemWindows="true" 實(shí)現(xiàn)沉浸式狀態(tài)欄效果。
android 手機(jī)頂部用于顯示各種通知和狀態(tài)信息的這個(gè)欄叫做狀態(tài)欄。通常情況下,我們應(yīng)用程序的內(nèi)容都是顯示在狀態(tài)欄下方的,但是為了更好的視覺效果,我們希望將應(yīng)用程序頂部的背景顏色延申到狀態(tài)欄的背后,這種效果就稱之為沉浸式狀態(tài)欄。
把a(bǔ)ndroid:fitsSystemWindows 屬性設(shè)置為 true 為打開沉浸式狀態(tài)欄效果,false 為關(guān)閉沉浸式狀態(tài)欄效果。同時(shí)需要在邏輯代碼里把狀態(tài)欄的背景改成透明色,才能看到效果。加上如下代碼:
getWindow().setStatusBarColor(Color.TRANSPARENT);
注意:android:fitsSystemWindows="true" 頂級(jí)布局必須是 CoordinatorLayout,否則也看不到效果。
到此這篇關(guān)于Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用的文章就介紹到這了,更多相關(guān)Android DrawerLayout與NavigationView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 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中NavigationView的使用與相關(guān)問題解決
相關(guān)文章
Android自定義雙向進(jìn)度條的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android自定義雙向進(jìn)度條的實(shí)現(xiàn)代碼,非常具有實(shí)用的價(jià)值,有興趣的同學(xué)一起來了解一下2017-09-09Android studio保存logcat日志到本地的操作
這篇文章主要介紹了Android studio保存logcat日志到本地的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04關(guān)于androidstuio導(dǎo)入系統(tǒng)源碼的問題
小編最近在做系統(tǒng)源碼導(dǎo)出來的小項(xiàng)目,在導(dǎo)入androidstudio過程中遇到過一些問題,本文以Schedule power on off為例給大家詳細(xì)介紹,需要的朋友參考下吧2021-06-06Android中TelephonyManager類的用法案例詳解
這篇文章主要介紹了Android中TelephonyManager類的用法,以獲取Android手機(jī)硬件信息為例詳細(xì)分析了TelephonyManager類的使用技巧,需要的朋友可以參考下2015-09-09