onCreateOptionsMenu 未被调用

onCreateOptionsMenu not being called

onCreateOptionsMenu 不会在片段中被调用。我必须在 onCreatedView() 中手动调用 setHasOptionsMenu(true) 但这会导致 item.itemIdonOptionsItemSelected() 中成为空字符串 "" 因此我无法检测到哪个菜单项目被点击。

我目前正在片段中使用它:

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, inflater)
    inflater.inflate(R.menu.menu_font_share, menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.menu_font_size_Btn -> {

        }
        R.id.menu_share_Btn -> {
            super.showShareSheet()
        }
    }

    return super.onOptionsItemSelected(item)
}

菜单展开后调用super.onCreateOptionsMenu(menu,inflater)

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.menu_font_share, menu)
    super.onCreateOptionsMenu(menu,inflater)
}

这可能更有帮助 参考 ::

试试这个,

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_sample, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

并在 onCreate 中添加此行以使选项显示在您的 Toolbar

setHasOptionsMenu(true);
 override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu, menu)
    return true
}

 override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle item selection
    return when (item.getItemId()) {
        R.id. menu_font_size_Btn -> {
           //your TODO
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
   }

要使 onCreateOptionsMenu 在您的片段中工作,您可以按照以下步骤操作:

确保将您的工具栏设置为操作栏(如果使用一个):

styles.xml

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    ...
</style>

在你的Activity的onCreate中:

setSupportActionBar(my_toolbar)

如果没有此行,onOptionsItemSelected 事件将不会 运行 在您的片段中。这会将所有 ActionBar 回调分配给 Toolbar。

在您的片段中 onViewCreatedonCreateView :

setHasOptionsMenu(true)

并重写此方法:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.id) {
        R.id.item_01 -> {}
        R.id.item_02 -> {}
        ...
    }
    super.onCreateOptionsMenu(menu, inflater)
}

如果您正在使用 Android 导航组件,请将此添加到您的 Activity:

setupActionBarWithNavController(navController)

Fragment-owned app bar:如果您在 Fragment 布局中定义了 Toolbar,而在父 [=] 布局中定义了 not 14=],您应该使用工具栏 API 来扩充菜单。 (片段菜单 API 仅适用于 activity 拥有的应用栏)

片段布局

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    ... />

片段

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    ...
    val toolbar = binding.toolbar
    toolbar.inflateMenu(R.menu.sample_menu)
    toolbar.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.action_settings -> {
                    // Navigate to settings screen
                    true
                }
                R.id.action_done -> {
                    // Save profile changes
                    true
                }
                else -> false
            }
        }

    //set item visibility
    toolbar.menu.findItem(R.id.action_done).isVisible = ...
    ...
}