首页 文章

Jetpack导航:操作栏中的 Headers 和后退/上箭头?

提问于
浏览
4

我安装了最新的金丝雀版Android Studio,并按照此(https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing)指令实现了简单的两页导航 . 基本上page1有一个按钮,当它被点击时,应用程序显示page2 .

它有效,但有一个问题......似乎没有自动对动作栏做任何事情 . 是否应该通过导航库自动在操作栏上显示/返回箭头和“标签”属性?或者我应该像以前一样手动完成所有工作?我想在第2页显示时显示后退箭头和动作(工具)栏上的“详细信息” .

在按钮上单击第1页 .

override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
    button1.setOnClickListener {
        val nav = NavHostFragment.findNavController(this);
        nav.navigate(R.id.show_page2)
    }
}

主要活动XML . 默认情况下,它是默认的操作栏,我用ToolBar替换它 . 没有区别 .

<?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:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        android:background="?attr/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_width="match_parent">
    </androidx.appcompat.widget.Toolbar>

    <fragment
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:navGraph="@navigation/nav_graph"/>

</androidx.constraintlayout.widget.ConstraintLayout>

导航图XML .

<?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/page1">

    <activity
        android:id="@+id/mainActivity2"
        android:name="com.android.navtest.MainActivity"
        android:label="activity_main"
        tools:layout="@layout/activity_main"/>
    <fragment
        android:id="@+id/page1"
        android:name="com.android.navtest.BlankFragment2"
        android:label="Home page"
        tools:layout="@layout/page1">
        <action
            android:id="@+id/show_page2"
            app:destination="@id/page2"
            app:enterAnim="@anim/anim1"
            app:popExitAnim="@anim/anim2"/>
    </fragment>
    <fragment
        android:id="@+id/page2"
        android:name="com.android.navtest.BlankFragment"
        android:label="Details"
        tools:layout="@layout/page2"/>
</navigation>

1 回答

  • 12

    您可以使用NavigationUI.setupActionBarWithNavController()将ActionBar连接到NavController . 这通常在您调用 setSupportActionBar() 之后立即在您的活动中完成:

    supportActionBar = findViewById<Toolbar>(R.id.toolbar)
    
    // Get the NavController for your NavHostFragment
    val navController = findNavController(R.id.nav_host_fragment)
    
    // Set up the ActionBar to stay in sync with the NavController
    setupActionBarWithNavController(navController)
    

    这种方法在Navigation talk at Google I/O 2018中有所介绍 .

相关问题