亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用

 更新時間:2023年01月06日 10:25:45   作者:別偷我的豬_09  
這篇文章主要介紹了Android DrawerLayout抽屜布局與NavigationView導(dǎo)航菜單應(yīng)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

現(xiàn)在 Android Studio 已經(jīng)直接提供左滑菜單功能,只需要在創(chuàng)建新項目時選擇 Navigation Drawer Activity 就可以直接創(chuàng)建一個有左滑菜單功能的 APP。

DrawerLayout

DrawerLayout 是 support-v4 Library 包中實現(xiàn)了側(cè)滑菜單效果的控件,可以說 drawerLayout 是因為第三方控件如 MenuDrawer 等的出現(xiàn)之后,Google 借鑒而出現(xiàn)的產(chǎn)物。DrawerLayout 分為側(cè)邊菜單和主內(nèi)容區(qū)兩個部分,側(cè)邊菜單可以根據(jù)手勢展開與隱藏(drawerLayout 自身特性),主內(nèi)容區(qū)可以隨著菜單的點擊而變化。

1. 抽屜式導(dǎo)航欄是顯示應(yīng)用主導(dǎo)航菜單的界面面板。當(dāng)用戶觸摸應(yīng)用欄中的抽屜式導(dǎo)航欄圖標(biāo)或從屏幕的左邊緣滑動手指時,就會顯示抽屜式導(dǎo)航欄;

2. 抽屜式導(dǎo)航欄圖標(biāo)會顯示在使用 DrawerLayout 的所有頂級目的地上。頂級目的地是應(yīng)用的根級目的地。它們不會在應(yīng)用欄中顯示向上按鈕。

3. 要添加抽屜式導(dǎo)航欄,請先聲明 DrawerLayout 為根視圖。在 DrawerLayout 內(nèi),為主界面內(nèi)容以及包含抽屜式導(dǎo)航欄內(nèi)容的其他視圖添加布局。

4. 例如,以下布局使用含有兩個子視圖的 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 的實現(xiàn)類,這里是 NavHostFragment;

app:defaaultNavHost="true" 攔截系統(tǒng)返回鍵,當(dāng)用戶按下返回鍵時,系統(tǒng)會將正在展示的 Fragment 返回。

app:navGraph 關(guān)聯(lián)導(dǎo)航圖;mobile_navigation.xml 是放在 res --> navigation 文件加里,是一個包含導(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 必填項,后面 fragment跳轉(zhuǎn)的時候要用到;

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 中,可以實現(xiàn)側(cè)滑效果的 UI 。DrawerLayout 布局可以有3個子布局,第一個布局必須是主界面而且不可以不寫,其他2個布局就是左、右兩個側(cè)滑布局,左右兩個側(cè)滑布局可以只寫其中一個。

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 下新建一個 menu 文件夾,activity_main_drawer.xml 是放在 menu 下的菜單布局文 件。里面是 Home / Gallery / Slideshow 等 item??梢栽O(shè)置每個 item 的 title、icon等。

app:itemIconTint 設(shè)置菜單圖標(biāo)的顏色;

app:itemTextColor 設(shè)置菜單文字的顏色;

app:itemBackground 設(shè)置菜單背景的顏色;

android:src與app:srcCompat

二者幾乎沒有區(qū)別,都是加載圖片資源。

app:srcCompat將矢量可繪制對象集成到應(yīng)用程序中。矢量可繪制對象允許您用XML中定義的單個矢量圖形替換多個png資產(chǎn)。從Android支持庫23.3.0開始,支持向量可繪制對象只能通過加載app:srcCompat。

app:srcCompat設(shè)置一個可繪制對象作為此ImageView的內(nèi)容,它將以其原始大小顯示。

fitsSystemWindows

android:fitsSystemWindows="true" 實現(xiàn)沉浸式狀態(tài)欄效果。

android 手機頂部用于顯示各種通知和狀態(tài)信息的這個欄叫做狀態(tài)欄。通常情況下,我們應(yīng)用程序的內(nèi)容都是顯示在狀態(tài)欄下方的,但是為了更好的視覺效果,我們希望將應(yīng)用程序頂部的背景顏色延申到狀態(tài)欄的背后,這種效果就稱之為沉浸式狀態(tài)欄。

把android:fitsSystemWindows 屬性設(shè)置為 true 為打開沉浸式狀態(tài)欄效果,false 為關(guān)閉沉浸式狀態(tài)欄效果。同時需要在邏輯代碼里把狀態(tài)欄的背景改成透明色,才能看到效果。加上如下代碼:

getWindow().setStatusBarColor(Color.TRANSPARENT);

注意:android:fitsSystemWindows="true" 頂級布局必須是 CoordinatorLayout,否則也看不到效果。

到此這篇關(guān)于Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用的文章就介紹到這了,更多相關(guān)Android DrawerLayout與NavigationView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論