我的问题是我的工具栏上的菜单项没有显示。具体来说,我有一个名为“设置”的菜单项,它封装了我想要在单击“设置”图标时显示的所有其他菜单项。但是,当单击该图标时,不会向用户显示其他菜单项的列表。
这是我的my_menu.xml
:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/settings"
app:showAsAction="always"
android:icon="@drawable/ic_settings"
android:title="@string/settings">
<menu>
<item
android:id="@+id/loginFragment"
android:menuCategory="secondary"
android:icon="@drawable/ic_login"
app:showAsAction="ifRoom"
android:title="@string/loginOrLogout"/>
<item
android:id="@+id/firstFragment"
android:icon="@drawable/ic_first_fragment_icon"
android:menuCategory="secondary"
app:showAsAction="ifRoom"
android:title="@string/first_fragment_text"/>
<item
android:id="@+id/secondFragment"
android:icon="@drawable/ic_second_fragment"
android:menuCategory="secondary"
app:showAsAction="ifRoom"
android:title="@string/second_fragment_text"/>
</menu>
</item>
</menu>
这是我的MainActivity.kt
课:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val navController by lazy{ findNavController(R.id.myNavHostFragment)}
private val appBarConfiguration by lazy {
AppBarConfiguration(setOf(R.id.firstFragment), binding.drawerLayout)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.lifecycleOwner = this
setSupportActionBar(binding.toolbar)
setupNavigationComponents(navController)
setupNavControllerListener()
}
private fun setupNavigationComponents(navController: NavController) {
binding.apply {
toolbar.setupWithNavController(navController, appBarConfiguration)
navView?.setupWithNavController(navController)
bottomNavView?.setupWithNavController(navController)
}
}
private fun setupNavControllerListener() {
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id in arrayOf(
R.id.firstFragment)){
toolbar.visibility = View.GONE
bottom_nav_view?.visibility = View.GONE
nav_view?.visibility = View.GONE
}else{
toolbar.visibility = View.VISIBLE
bottom_nav_view?.visibility = View.VISIBLE
nav_view?.visibility = View.VISIBLE
}
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.my_menu, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean =
/*
* Have the NavigationUI look for an action or destination matching the menu
* item id and navigate there if found.
* Otherwise, bubble up to the parent.
* */
item.onNavDestinationSelected(findNavController(R.id.myNavHostFragment))
|| super.onOptionsItemSelected(item)
override fun onSupportNavigateUp(): Boolean {
// Allows NavigationUI to support proper up navigation or the drawer layout
// drawer menu, depending on the situation
return findNavController(R.id.myNavHostFragment).navigateUp(appBarConfiguration)
}
}
在下面,我显示了屏幕右上角的屏幕截图,您可以在其中看到“设置”图标:
当我单击该图标时,我得到以下结果:
如您所见,没有向用户显示任何菜单项。奇怪的是,当我单击“打开的白框”内的某个位置时,应用程序会导航到相应的片段。但是项目没有显示,为什么?
我在这里做错了什么?