Code setting style for Actionbar and menu in Android

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.