欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

現(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android自定義雙向進(jìn)度條的實(shí)現(xiàn)代碼

    Android自定義雙向進(jìn)度條的實(shí)現(xiàn)代碼

    本篇文章主要介紹了Android自定義雙向進(jìn)度條的實(shí)現(xiàn)代碼,非常具有實(shí)用的價(jià)值,有興趣的同學(xué)一起來了解一下
    2017-09-09
  • Android studio保存logcat日志到本地的操作

    Android studio保存logcat日志到本地的操作

    這篇文章主要介紹了Android studio保存logcat日志到本地的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • 關(guān)于androidstuio導(dǎo)入系統(tǒng)源碼的問題

    關(guān)于androidstuio導(dǎo)入系統(tǒng)源碼的問題

    小編最近在做系統(tǒng)源碼導(dǎo)出來的小項(xiàng)目,在導(dǎo)入androidstudio過程中遇到過一些問題,本文以Schedule power on off為例給大家詳細(xì)介紹,需要的朋友參考下吧
    2021-06-06
  • 詳談閃屏頁相關(guān)處理

    詳談閃屏頁相關(guān)處理

    本文主要介紹了閃屏頁相關(guān)處理方面的知識(shí),具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • Android Retrofit框架的使用

    Android Retrofit框架的使用

    這篇文章主要介紹了Android Retrofit框架的使用,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android中TelephonyManager類的用法案例詳解

    Android中TelephonyManager類的用法案例詳解

    這篇文章主要介紹了Android中TelephonyManager類的用法,以獲取Android手機(jī)硬件信息為例詳細(xì)分析了TelephonyManager類的使用技巧,需要的朋友可以參考下
    2015-09-09
  • Android UI開發(fā) View自繪控件 分享

    Android UI開發(fā) View自繪控件 分享

    Android UI開發(fā) View自繪控件 分享,需要的朋友可以參考一下
    2013-05-05
  • Android開發(fā)常用經(jīng)典代碼段集錦

    Android開發(fā)常用經(jīng)典代碼段集錦

    這篇文章主要介紹了Android開發(fā)常用經(jīng)典代碼段,涉及Android開發(fā)過程中針對(duì)手機(jī)、聯(lián)系人、圖片、存儲(chǔ)卡等的相關(guān)操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2016-02-02
  • Android實(shí)現(xiàn)圖片文字輪播特效

    Android實(shí)現(xiàn)圖片文字輪播特效

    這篇文章主要介紹了Android圖片文字輪播效果,分別實(shí)現(xiàn)圖片和文字自動(dòng)滾動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android制作微信添加多個(gè)圖片放大圖片功能

    Android制作微信添加多個(gè)圖片放大圖片功能

    這篇文章主要介紹了Android制作微信添加多個(gè)圖片放大圖片功能,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-03-03

最新評(píng)論