合并TabActivity菜单,包含活动菜单 [英] Merge TabActivity menu with contained Activities menus

查看:151
本文介绍了合并TabActivity菜单,包含活动菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的主要活动是一个TabActivity,它包含一个OptionsMenu。我定义了一些其他活动(其中进入一个选项卡),我想定义菜单在这些活动中,并与主要的一个它的菜单合并。这可能吗?

My application's main Activity is a TabActivity and it contains an OptionsMenu. I define some other Activities (which go into a tab) and I would like to define a menu in each of these activities and have its menu merged with the main one. Is it possible?

推荐答案

是的,这是可能的。基本上你只是抬高多个XML文件到同一个选项菜单。项目添加到菜单,以便通货膨胀。

Yes, this is possible. Basically you just inflate multiple xml files into the same options menu. Items are added to the menu in order of inflation.

只是简单地覆盖 onCreateOptionsMenu(菜单菜单) TabActivity ,充气包含主选项的XML文件。然后覆盖它为每个标签内的活动,膨胀的标签特定的选项。只是写他们,你通常会:

Just overwrite onCreateOptionsMenu(Menu menu) for your TabActivity, inflating the xml file containing the main options. Then overwrite it for each of your inner tab activities, inflating the tab-specific options. Just write them as you usually would:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options, menu);
    return super.onCreateOptionsMenu(menu);
}

菜单属于当前激活的内部标签的活动,但来填充它, onCreateOptionsMenu (由被自动调用父的活动过多)。

The menu 'belongs' to the currently active inner tab activity, but to populate it, onCreateOptionsMenu is automatically called on the parent activities too (by super).

然而,奇怪的是, onMenuItemSelected(INT FEATUREID,菜单项项)并没有这样做。为了处理项目选择,内层标签活动,仍然必须显式调用父活动相应的方法(在确定所选择的选项不是标签专用):

However, strangely, onMenuItemSelected(int featureId, MenuItem item) does not do the same. To handle item selection, the inner tab activities still have to explicitly call the corresponding method on the parent activity (after you determine that the selected option is not tab-specific):

return super.onMenuItemSelected(featureId, item)
    || getParent().onMenuItemSelected(featureId, item);

这篇关于合并TabActivity菜单,包含活动菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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