Navigation的使用

Navigation


使用條件: AndroidStudio version >= 3.3

一種交互允許用戶在應用程序中的不同內容之間進行瀏覽,瀏覽和退出。

導航組件由以下三個關鍵部分組成:

  • 導航圖:在一個集中位置包含所有與導航有關的信息的一種XML資源。這包括您應用程序中的所有單獨內容區域(稱爲destinations),以及用戶可以通過您的應用程序訪問的可能路徑。

  • NavHost:一個空容器,用於顯示導航圖中的目的地。導航組件包含一個默認NavHost實現 NavHostFragment,它顯示片段目的地。

  • NavController: 在NavHost中管理應用程序導航的對象 。 當用戶在整個應用程序中移動時,NavController在NavHost中協調目標內容的交換 。

    MainActivity中 activity_main.xml 資源:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints"
        />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:ignore="MissingConstraints"
        app:defaultNavHost= "true"
        app:navGraph="@navigation/nav_graph"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
  • android:name屬性包含您的NavHost 實現的類名稱。
  • app:navGraph屬性將關聯到NavHostFragment導航圖。導航圖指定了NavHostFragment用戶可以導航到的所有目的地。
  • app:defaultNavHost="true"屬性確保您NavHostFragment 攔截系統的“後退”按鈕。請注意,NavHost默認值只能是一個。如果您在同一佈局中有多個主機(例如,兩窗格佈局),請確保僅指定一個default NavHost

建立導航圖

  1. 在“項目”窗口中,右鍵單擊res目錄,然後選擇“ 新建”>“ Android Resource File ”。出現“ 新資源文件”對話框。
  2. 在“ File name ”字段中鍵入一個名稱,例如“ nav_graph”。
  3. 從資源類型下拉列表中選擇 Navigation ,然後單擊確定
<?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/nav_graph">

</navigation>

建立、連接目的地

通過nav_graph中的Design 選項,點擊白綠“+”,出現"Create new destination",可以建立Fragment

然後可以通過“ 房子 ”符號或者右鍵set as Start destination建立啓動目的地

連接目的地,只要選中Design區域的Fragment頁,右側會出現圓形按鈕,按需求連線建立對應關係就可以了

例如:

<?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/nav_graph"
    app:startDestination="@id/blankFragment">

    <fragment
        android:id="@+id/blankFragment"
        android:name="com.fight.navigation.BlankFragment"
        android:label="fragment_blank"
        tools:layout="@layout/fragment_blank" >
        <action
            android:id="@+id/action_blankFragment_to_whiteFragment"
            app:destination="@id/whiteFragment" />
    </fragment>
    <fragment
        android:id="@+id/whiteFragment"
        android:name="com.fight.navigation.BlankFragment"
        android:label="fragment_white"
        tools:layout="@layout/fragment_blank">
      
    </fragment>

</navigation>

導航到目的地

Java

手勢導航

從Android 10(API級別29)開始,Android系統支持完全基於手勢的導航。應用程序開發人員應做兩件事以確保其應用程序與此功能兼容:

  • 將應用程序內容從一個邊緣擴展到另一個邊緣。
  • 處理衝突的應用手勢
邊緣到邊緣的應用程序內容

爲了利用浮動導航欄提供的額外屏幕空間,您需要對應用程序進行一些更改。

設置透明系統欄

您可以通過在主題中設置以下值來實現( API目標級別爲29或更高 ):

邊緣到邊緣的應用程序內容
爲了利用浮動導航欄提供的額外屏幕空間,您需要對應用程序進行一些更改。

設置透明系統欄
您可以通過在主題中設置以下值來實現:

<!-- values-29/themes.xml: -->

<style name="AppTheme" parent="...">
    <item name="android:navigationBarColor">@android:color/transparent</item>

    <!-- Optional, but recommended for full edge-to-edge rendering -->
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

或者 可以使用Window.setNavigationBarColor()Window.setStatusBarColor() 來動態地執行此操作

設置UI可見性標誌

爲了能夠邊到邊佈置視圖,應用必須告知系統該應用可以處理這種視圖。您可以通過View.setSystemUiVisibility() 設置以下標誌來完成此操作 :

STABLE)

這些標誌一起告訴系統,你的應用程序應該全屏顯示,就像導航欄和狀態欄不存在一樣。對於其他全屏事件,您還可以設置SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN,它允許您在狀態欄後面繪圖。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章