<Menuxmlns:android= "Http://schemas.android.com/apk/res/android"> <GroupAndroid:id= "@+id/menu_items"> <ItemAndroid:id= "@+id/menu_item_night_mode"Android:title= "@string/menu_item_night_mode"Android:icon= "@android:d rawable/ic_menu_preferences"/> <ItemAndroid:id= "@+id/menu_item_settings"Android:title= "@string/menu_item_settings"Android:icon= "@android:d rawable/ic_menu_preferences"/> <ItemAndroid:id= "@+id/menu_item_help"Android:title= "@string/menu_item_help"Android:icon= "@android:d rawable/ic_menu_preferences"/> </Group></Menu>
@Override Public BooleanOncreateoptionsmenu (Menu menu) {//We only have want to show it as a menu in landscape, and only for clock/alarm fragment.Mmenu =menu; //Direction Level if(Getresources (). GetConfiguration (). Orientation = =Configuration.orientation_landscape) { if(Mactionbar.getselectednavigationindex () = = Alarm_tab_index | |Mactionbar.getselectednavigationindex ()==Clock_tab_index) { //Clear The menu so it doesn ' t get duplicate items in case Oncreateoptionsmenu//Was called multiple times.menu.clear (); Getmenuinflater (). Inflate (R.menu.desk_clock_menu, menu); MenuItem Nightmode=Menu.finditem (R.id.menu_item_night_mode); MenuItem Help=Menu.finditem (R.ID.MENU_ITEM_HELP); if(Mactionbar.getselectednavigationindex () = =Alarm_tab_index) {nightmode.setvisible (false); Help.setvisible (false); } Else if(Mactionbar.getselectednavigationindex () = =Clock_tab_index) {nightmode.setvisible (true); Help.setvisible (false); } } //Always return true to landscape, regardless of whether we ve inflated the menu, so//That's when we switch tabs This method would get called and we can inflate the menu. return true; } return false; } @Override Public BooleanOnprepareoptionsmenu (Menu menu) {updatemenu (menu); return true; } Private voidUpdatemenu (Menu menu) {//Hide "Help" If we don has a URI for it.MenuItem help =Menu.finditem (R.ID.MENU_ITEM_HELP); if(Help! =NULL) {Utils.preparehelpmenuitem ( This, help); } //Hide ' Lights Out ' for timer.MenuItem Nightmode =Menu.finditem (R.id.menu_item_night_mode); if(Mactionbar.getselectednavigationindex () = =Alarm_tab_index) {nightmode.setvisible (false); } Else if(Mactionbar.getselectednavigationindex () = =Clock_tab_index) {nightmode.setvisible (true); }} @Override Public Booleanonoptionsitemselected (MenuItem item) {if(Processmenuclick (item)) {return true; } return Super. onoptionsitemselected (item); } Private BooleanProcessmenuclick (MenuItem item) {Switch(Item.getitemid ()) { Caser.id.menu_item_settings:startactivity (NewIntent (Deskclock. This, Settingsactivity.class)); return true; Caser.id.menu_item_help:intent i=item.getintent (); if(I! =NULL) { Try{startactivity (i); } Catch(activitynotfoundexception e) {//No activity found to match the Intent-ignore } } return true; Caser.id.menu_item_night_mode:startactivity (NewIntent (Deskclock. This, Screensaveractivity.class)); default: Break; } return true; }
Android Menu Dynamic Change "Add or remove"