Android如何为不同的选项卡添加多个菜单

Android如何为不同的选项卡添加多个菜单,android,android-tabhost,android-tabs,android-tabactivity,Android,Android Tabhost,Android Tabs,Android Tabactivity,我有很多这样的标签: TabHost th; TabSpec spec; th = (TabHost) findViewById(R.id.tabhost_template_two_tabs); th.setup(); // all tab spec = th.newTabSpec("All"); spec.setIndicator("All"); spec.setContent(R.id.tab_template_two_tabs_all); th.addTab(spec); // favor

我有很多这样的标签:

TabHost th;
TabSpec spec;
th = (TabHost) findViewById(R.id.tabhost_template_two_tabs);
th.setup();
// all tab
spec = th.newTabSpec("All");
spec.setIndicator("All");
spec.setContent(R.id.tab_template_two_tabs_all);
th.addTab(spec);
// favorite tab
spec = th.newTabSpec("Favorite");
spec.setIndicator("Favorite");
spec.setContent(R.id.tab_template_two_tabs_favorite);
th.addTab(spec);

对于每个选项卡,我想添加一个菜单,如何添加?

TabHost
中的每个
tab
都有一个从0开始的数字,因此您必须使用
int currentTab=th.getCurrentTab()知道您现在在哪个选项卡中然后使用
menu.clear()
清除以前的菜单,然后使用
inflater.inflater(menuID,menu)

注:
oncreateoptions菜单对您的情况没有帮助

TabHost
中的每个
选项卡都有一个从0开始的数字,因此您必须使用
int currentTab=th.getCurrentTab()知道您现在在哪个选项卡中然后使用
menu.clear()
清除以前的菜单,然后使用
inflater.inflater(menuID,menu)

注:
onCreateOptions菜单
对您的情况没有帮助

菜单是指单击硬件菜单按钮时弹出的菜单?菜单是指单击硬件菜单按钮时弹出的菜单?
@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater inflater = getMenuInflater();
        int currentTab = th.getCurrentTab();
        Toast.makeText(getApplicationContext(), currentTab+"", Toast.LENGTH_SHORT);
        if (currentTab == 0){
            menu.clear();
            inflater.inflate(R.menu.first, menu);}
        else{
            menu.clear();
            inflater.inflate(R.menu.second, menu);}
        return super.onPrepareOptionsMenu(menu);
    }