This article mainly introduces the Android Actionbar and menu code settings of the relevant information, the need for friends can refer to the
Menu Part XML code
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
<?xml version= "1.0" encoding= "Utf-8"?> <menu XM Lns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/action_search" android:title= "Search 1" android:orderincategory= "android:showasaction=" Always "/> <item android:id=" @+ Id/action_search2 "android:actionviewclass=" Android.widget.SearchView "android:showasaction=" ifroom| Collapseactionview "android:orderincategory=" android:title= "Search 2"/> <item android:id= "@+id/ Action_share "android:title=" Share "android:orderincategory=" android:icon= "@drawable/ic_action_favor_normal" android:showasaction= "Never"/> <item android:id= "@+id/action_collection" android:title= "collection" Android:o rderincategory= "android:showasaction=" "Never"/> <item android:id= "@+id/action_font" Android:title= "Font size" android:orderincategory= "android:showasaction=" never/> </menu> |
Menu icon Display Implementation "Rewrite onmenuopened method, use reflection principle" overflower
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 |
/** * Display overflower menu icon */@Override public boolean onmenuopened (int featureid, menu menu) {if (Featureid = = Window.featur E_action_bar && menu!= null) {if (Menu.getclass (). Getsimplename (). Equals ("Menubuilder")) {try {method M = Menu . GetClass (). Getdeclaredmethod ("setoptionaliconsvisible", Boolean.type); M.setaccessible (TRUE); M.invoke (menu, true); catch (Exception e) {}} return super.onmenuopened (Featureid, menu); } |
Event listener action for selected items in the menu menu
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 |
/** * Menu click Action Monitor Event/@Override public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {Case A Droid. R.id.home://finish (); Super.onbackpressed (); Break Case R.id.action_add:toast.maketext (this, add, Toast.length_short). Show (); Break return super.onoptionsitemselected (item); } |
Custom Actionbar
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
Implementation effect:
The above mentioned is the entire content of this article, I hope you can enjoy.