如何在具有导航组件的单个片段上适当地添加选项菜单而又不破坏“举止行为"? [英] How to properly add options menu on a single fragment with navigation component without broke "up behavior"
问题描述
我在单个片段上添加选项菜单遇到了一些麻烦,因为它破坏了导航.这是我的代码
I'm having some trouble to add options menu on a single fragment because it's breaking the navigation Up. Here my code
我有一个具有 NoActionBar
样式和这种布局的Activity
I have an single Activity with NoActionBar
style and with this layout
<layout 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:fitsSystemWindows="true"
tools:context=".ui.MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<fragment
android:id="@+id/mainNavigationFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/main_graph" />
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:labelVisibilityMode="labeled"
app:menu="@menu/main_bottom_nav" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
活动 onCreate
中,我进行了导航设置
in activity onCreate
I make this setup for navigation
private fun setupNavigation() {
val navController = findNavController(R.id.mainNavigationFragment)
//each fragment of botton nav
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.actionSchedule,
R.id.actionPayment,
R.id.actionNotification,
R.id.actionAccount))
toolbar.setupWithNavController(navController, appBarConfiguration)
bottomNavigationView.setupWithNavController(navController)
}
override fun onSupportNavigateUp() =
findNavController(R.id.mainNavigationFragment).navigateUp()
在每个botton导航片段上,我都有一些目的地,并且一切正常.
on each botton nav fragment I have some destinations and everything work as expected.
现在,我只需要在botton导航的最右边片段上添加一个菜单,然后在此特定片段上,在onCreate中添加 setHasOptionsMenu(true)
,并在 onCreateOptionsMenu
,但菜单不会出现.
Now I need to add an menu only on right most fragment of botton nav, then on this specific fragment I add setHasOptionsMenu(true)
in onCreate and inflate menu in onCreateOptionsMenu
, but the menu does not appear.
然后在活动 onCreate
上添加 setSupportActionBar(toolbar)
.
现在,菜单仅出现在此片段上,但是它破坏了任何目标位置的所有"UP"(工具栏上的后退箭头)(后退箭头出现,但是当我按下时什么也没发生).如果我删除活动的 setSupportActionBar(toolbar)
,则UP可以再次工作,但不能再次使用工具栏菜单.
Now the menu appear only on this fragment but it's broke all 'UP' (back arrow on toolbar) of any destination (the back arrow appears, but when i press nothing happens). If I remove setSupportActionBar(toolbar)
of activity the UP work again but not the toolbar menu.
我需要做些什么才能使菜单仅在一个片段中起作用而不会破坏其他任何东西?谢谢
What I need to do to make the menu work only in one fragment without broke anything else? Thanks
推荐答案
如果使用的是 setSupportActionBar
,则必须使用 setupActionBarWithNavController()
,而不是 toolbar.setupWithNavController
根据文档.
If you're using setSupportActionBar
, you must use setupActionBarWithNavController()
, not toolbar.setupWithNavController
as per the documentation.
这篇关于如何在具有导航组件的单个片段上适当地添加选项菜单而又不破坏“举止行为"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!