Android 如何在选项卡活动的片段中使用 onCreateOptionsMenu?(夏洛克动作条)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11855581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:01:26  来源:igfitidea点击:

How can I use onCreateOptionsMenu in a Fragment within a tab Activity? (Sherlock ActionBar)

androidandroid-fragmentsactionbarsherlockandroid-optionsmenu

提问by snotyak

I have the following method overridden in my tab Activity and it works fine like that but I want specific option menu's for each fragment. When I put this in my fragment and press the menu button, nothing happens.

我在我的标签 Activity 中覆盖了以下方法,它可以正常工作,但我想要每个片段的特定选项菜单。当我把它放在我的片段中并按下菜单按钮时,没有任何反应。

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.activity_calc, menu);
    return true;
}

回答by CommonsWare

You need to call setHasOptionsMenu(true)on your fragment before it can participate in the action bar.

您需要先调用setHasOptionsMenu(true)您的片段,然后它才能参与操作栏。

回答by Sparky

May I suggest you check out http://code.google.com/p/sherlock-demo/. As BackpackOnHead describes, it uses ViewPager + ActionBar Tabs to let you navigate two different ways between the Fragments in an Activity. In this case, it is the FragmentTabs portion of the API Demos sample app, ported to ActionBarSherlock. The LoaderThrottle Fragment in particular has its own OptionsMenu items.

我建议您查看http://code.google.com/p/sherlock-demo/。正如 BackpackOnHead 所描述的,它使用 ViewPager + ActionBar Tabs 让您在 Activity 中的 Fragment 之间导航两种不同的方式。在本例中,它是 API Demos 示例应用程序的 FragmentTabs 部分,移植到 ActionBarSherlock。特别是 LoaderThrottle Fragment 有它自己的 OptionsMenu 项。