ActionBar中的onPrepareOptionsMenu复制项 [英] onPrepareOptionsMenu Duplicates item in ActionBar
问题描述
当我使用onPrepareOptionsMenu添加菜单项时,该菜单项会在操作栏中复制其自身.我正在使用片段并在主要活动的ActionBar中创建初始菜单,如下所示:
When I add a menu item using onPrepareOptionsMenu, the menu item duplicates its self in the action bar. I'm using fragments and creating the initial menu in the ActionBar in the main activity like this:
...
@Override
public boolean onCreateOptionsMenu(Menu paramMenu) {
super.onCreateOptionsMenu(paramMenu);
paramMenu.add(0, 1, 0, "DashBoard").setIcon(R.drawable.ic_dashboard)
.setShowAsAction(1);
return true;
}
然后我在其中一个片段中添加另一个项目,如下所示:
I'm then adding another item in one of the fragments as follows:
...
@Override
public void onPrepareOptionsMenu(Menu paramMenu) {
paramMenu.add(0, 2, 1, "FullScreen").setIcon(R.drawable.ic_fullscreen)
.setShowAsAction(1);
}
由于某种原因,通过片段类添加的项显示两次....我有什么问题吗?
For some reason this added item via the fragment class displays twice.... Do i have something wrong?
任何对我所犯错误的帮助都会受到赞赏
Any help to what I have wrong will be appreciated
推荐答案
该项目可能显示两次,因为您将其添加了两次.请参见文档以获取onPrepareOptionsMenu
:
The item is probably displaying twice because you're adding it twice. See the docs for onPrepareOptionsMenu
:
在每次显示菜单之前都会调用它.
This is called right before the menu is shown, every time it is shown.
我真的不会盲目地在onPrepareOptionsMenu
中添加项目.您应该检查它是否已经被首先添加.
I really wouldn't blindly add an item in onPrepareOptionsMenu
ever. You should check if it's already been added first.
这篇关于ActionBar中的onPrepareOptionsMenu复制项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!