0
我的應用程序中有兩個標籤,我希望菜單根據標籤進行更改。在Android中的標籤之間切換時的更新菜單
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);
}
if (currentTab ==1){
menu.clear();
inflater.inflate(R.menu.second, menu);
}
return super.onPrepareOptionsMenu(menu);
}
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
closeOptionsMenu();
}});
現在,當我切換到歌曲標籤時,照片菜單仍然存在,直到我點擊它然後出現歌曲菜單。我想要菜單得到更新,一旦我點擊標籤
提供一個快照以便更好地理解。 –
'onTabChanged'中'tableId'的值是什麼?它與當前標籤或下一個標籤是否相等? –