我的应用程序中有两个选项卡,我希望菜单根据选项卡进行更改。
这是我所做的
TabHost tabHost = tabHost = getTabHost();
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.photo));
Intent photosIntent = new Intent(this, Photos.class);
photospec.setContent(photosIntent);
TabSpec songspec = tabHost.newTabSpec("Songs");
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.songs));
Intent songsIntent = new Intent(this, Songs.class);
songspec.setContent(songsIntent);
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
现在,当用户单击“照片”选项卡时,我想显示一个用于编辑图片的菜单,当用户单击“歌曲”选项卡时,我想显示一个控制歌曲顺序的菜单。我想在每次用户单击任何选项卡时执行此操作。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int currentTab = tabHost.getCurrentTab();
if (currentTab == 0)
startActivity(new Intent(this, Photosoptions.class));
if (currentTab == 1)
{
startActivity(new Intent(this, Songsoptions.class));
}
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
int currentTab = tabHost.getCurrentTab();
if (currentTab == 0){
menu.clear();
inflater.inflate(R.menu.first, menu);
closeOptionsMenu();
}
if (currentTab ==1){
menu.clear();
inflater.inflate(R.menu.second, menu);
closeOptionsMenu();
}
return super.onPrepareOptionsMenu(menu);
}
您可以使用以下代码:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
int currentTab = tabHost.getCurrentTab();
Toast.makeText(getApplicationContext(), currentTab+"", Toast.LENGTH_SHORT);
menu.clear();
if (currentTab == 0) {
inflater.inflate(R.menu.first, menu); // menu for photospec.
} else {
inflater.inflate(R.menu.second, menu); // menu for songspec
}
return super.onPrepareOptionsMenu(menu);
}
你不需要onCreateOptions菜单并且您必须处理项目点击onOptionsItemSelected
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)