是否可以使没有id的menuItem不可见。我有一个导航抽屉,它可以工作,但是没有XML文件。 通常你喜欢这样
MenuItem item = findViewById()
item.setVisible(false);
我的代码
private NavigationItem[] createNavigation(String[] names){
Log.d(TAG, "createNavigation");
navItems = new NavigationItem[names.length];
for(int i = 0; i < names.length; i++){
Navigation navTo = Navigation.MyPage;
if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[0]) == 0){
navTo = Navigation.MyPage;
if(currentFragment instanceof MyPageFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_mypage_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_mypage_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[1]) == 0){
navTo = Navigation.Book;
if(currentFragment instanceof BookFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_book_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_book_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[2]) == 0){
navTo = Navigation.Status;
if(currentFragment instanceof StatusFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_status_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_status_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[3]) == 0){
navTo = Navigation.Information;
if(currentFragment instanceof InfoFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_info_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_info_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[4]) == 0){
navTo = Navigation.Settings;
if(currentFragment instanceof SettingsFragment)
navItems[i] = new NavigationItem(names[i], R.drawable.ic_setting_norm, navTo);
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_setting_norm, navTo);
}
else if(names[i].compareTo(getResources().getStringArray(R.array.navigation)[5]) == 0){
navTo = Navigation.PanoramaHome;
if(currentFragment instanceof PanoramaHomeFragment){
navItems[i] = new NavigationItem(names[i], R.drawable.ic_setting_norm, navTo);
Log.d(TAG, "createNavigation: if");
}
else
navItems[i] = new NavigationItem(names[i], R.drawable.ic_setting_norm, navTo);
Log.d(TAG, "createNavigation: else");
}
//navItems[i] = new NavigationItem(names[i], R.drawable.pholder_icon, navTo);
}
return navItems;
}
void navigateTo(NavigationItem ni){
Log.d(TAG, "navigateTo");
Fragment newFragment = null;
String fragTag = null;
if(ni.isMyPage()){
fragTag = getString(R.string.mypage);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new MyPageFragment();
}
else if(ni.isBooking()){
fragTag = getString(R.string.booking);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new BookFragment();
}
else if(ni.isStatus()){
fragTag = getString(R.string.status);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new StatusFragment();
}
else if(ni.isInfo()){
fragTag = getString(R.string.info);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new InfoFragment();
}
else if(ni.isSettings()){
fragTag = getString(R.string.settings);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new SettingsFragment();
}
//TODO CHANGE TO PANORAMA HOME Fragment.
else if(ni.isPanoramaHome()){
fragTag = getString(R.string.panorama_home);
newFragment = fragMan.findFragmentByTag(fragTag);
if(newFragment == null)
newFragment = new PanoramaHomeFragment();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
currentFragment = newFragment;
transaction.replace(R.id.main_fragment, newFragment, fragTag);
transaction.addToBackStack(null);
transaction.commit();
drawerLayout.closeDrawer(drawerList);
supportInvalidateOptionsMenu();
navDrawerSetup();
setLoading(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptionsMenu");
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
if(currentFragment != null){
if(currentFragment instanceof BookFragment){
CacheController ch = new CacheController(this);
ch.openDatabases();
if(ch.getAvailableLocalPreChoices() != 1)
inflater.inflate(R.menu.bookmenu, menu);
ch.closeDatabases();
}
else if(currentFragment instanceof StatusFragment || currentFragment instanceof InfoFragment || currentFragment instanceof MyPageFragment){
inflater.inflate(R.menu.refreshmenu, menu);
}
else{
inflater.inflate(R.menu.nomenu, menu);
}
//need id for the fragment to work.
//MenuItem item = findViewById()
//item.setVisible(false);
MenuItem m = menu.getItem(1);
m.setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
答案 0 :(得分:0)
您可以使用以下位置更改菜单项的可见性:-
MenuItem m = menu.getItem(2);
m.setVisible(false);
我希望这对您有用@silversoul