片段,用相同的菜单中,在相同的布局原因重复菜单项 [英] Fragments, with the same menu, on the same layout cause duplicated menuitem

查看:126
本文介绍了片段,用相同的菜单中,在相同的布局原因重复菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局其中有2个片段。这些片段使用相同的菜单,因为我有另一种布局,只有其中一个(在小屏幕设备中使用)。

I have a layout which has 2 fragments. These fragments uses the same menu, because I have another layouts that has only one of these (used in small screen device).

我的问题是每个片段的菜单项显示在菜单中,并且由于它们是从同一个菜单中,它们被复制到彼此。我需要从一个片段,但不能同时只显示菜单。反正我有可以存档吗?

My problem is menu items of each fragments shows in the menu, and since they're from the same menu, they are duplicated to each others. I need to display only menu from one fragment, not both. Is there anyway I could archive this ?

或者,可能会更好,创建2个不同的菜单。一个用于具有单独这些片段(其是用在小屏幕装置)中的一个的布局,而另一个为具有两个片段布局。我怎么能这样做呢?

Or, probably better, create 2 different menu. One for the layouts that has one of these fragment alone (which is used in small-screen device), and another one for layout having both fragments. How could I do this ?

编辑:我使用ActionBarSherlock库向后兼容性,我测试它在Android 3.0模拟器

I use ActionBarSherlock library for backward compatibility, and I test it on the Android 3.0 emulator.

推荐答案

我已经找到了不那么好的解决方案。在每一个片段onCreateOptionsMenu()调用menu.clear(),以消除任何现有的菜单项前膨胀菜单。

I've found the not-so-good solution. In onCreateOptionsMenu() of each fragment, call menu.clear() to remove any existing menu item before inflate the menu.

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

}

这会后的未来的问题。我可以决定使用不同的菜单每一个片段,其中每个菜单共享菜单项。我认为,我们可以在onCreateOptionsMenu()操作菜单,但现在我不知道怎么去与片段相关的菜单项之前充气用MenuInflater。

This would post the future problems. I may decide to uses different menus for each fragments, which each menus share menu items. I think we can manipulate the menu at the onCreateOptionsMenu(), but currently I don't know how to get the menu item associated with the fragment before inflate it with MenuInflater.

这篇关于片段,用相同的菜单中,在相同的布局原因重复菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆