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

查看:13
本文介绍了将 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.

只需为您的 TabActivity 覆盖 onCreateOptionsMenu(Menu menu),膨胀包含主要选项的 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(通过 super).

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, MenuItem item) 并没有这样做.为了处理项目选择,内部选项卡活动仍然必须显式调用父活动上的相应方法(在您确定所选选项不是特定于选项卡之后):

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天全站免登陆