Code setting styles for Android Actionbar and menu

Source: Internet
Author: User

Menu Part XML code

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Menuxmlns:android= "Http://schemas.android.com/apk/res/android" >3     4     <ItemAndroid:id= "@+id/action_search"5 Android:title= "Search 1"6 android:orderincategory= "+"7 android:showasaction= "Always"/>8     9     <ItemAndroid:id= "@+id/action_search2"Ten Android:actionviewclass= "Android.widget.SearchView" One android:showasaction= "Ifroom|collapseactionview" A android:orderincategory= "+" - Android:title= "Search 2"/> -      the      -     <ItemAndroid:id= "@+id/action_share" - Android:title= "Share" - android:orderincategory= "+" + Android:icon= "@drawable/ic_action_favor_normal" - android:showasaction= "Never"/> +     <ItemAndroid:id= "@+id/action_collection" A Android:title= "Favorites" at android:orderincategory= "+" - android:showasaction= "Never"/> -     <ItemAndroid:id= "@+id/action_font" - Android:title= "Font Size" - android:orderincategory= "+" - android:showasaction= "Never"/> in </Menu>

Overflower Menu icon Display Implementation "Overriding onmenuopened method, using Reflection principle"

1 /**2 * Display overflower menu icon3      */4 @Override5      Public BooleanOnmenuopened (intFeatureid, Menu menu) {  6         if(Featureid = = Window.feature_action_bar && Menu! =NULL) {  7             if(Menu.getclass (). Getsimplename (). Equals ("Menubuilder")) {  8                 Try {  9Method m = Menu.getclass (). Getdeclaredmethod ("Setoptionaliconsvisible", Boolean.type); TenM.setaccessible (true);  OneM.invoke (menu,true);  A}Catch(Exception e) { -                 }   -             }   the         }   -         return Super. onmenuopened (Featureid, menu);  -}

Event listener Action for options in menu menus

1     /**2 * Menu click Action Listener Event3      */4 @Override5      Public Booleanonoptionsitemselected (MenuItem item) {6         Switch(Item.getitemid ()) {7          CaseAndroid. R.id.home:8             //finish ();9             Super. onbackpressed ();Ten              Break; One          CaseR.id.action_add: AToast.maketext ( This, "Add", Toast.length_short). Show (); -              Break; -         } the         return Super. onoptionsitemselected (item); -}

Custom Actionbar

1 /**2 * Initialization of Actionbar content3      * */4     PrivateActionBar ActionBar;5     Private voidInitactionbar () {6Actionbar=Super. Getactionbar ();7 actionbar.show ();8          9          //Show Home AreaTenActionbar.setdisplayshowhomeenabled (true); One          //Set Home Area fallback button AActionbar.setdisplayhomeasupenabled (true); - Actionbar.sethomeasupindicator (r.drawable.back_move_details_normal); -           the          //do not show home area title -Actionbar.setdisplayshowtitleenabled (true);// -Actionbar.settitle ("News");//Set Title -          //do not display logo image +Actionbar.setdisplayuselogoenabled (false);//          -          //Remove icon Icon "Set icon color to transparent" in the home area +Drawable colordrawable=New  A colordrawable (Android. r.color.transparent); at Actionbar.seticon (colordrawable); -           -          //Custom Area -Actionbar.setdisplayshowcustomenabled (true); -TextView tvtitle=NewTextView ( This);//This , the currently hosted -Tvtitle.settext ("News Information");//Tvtitle.setid (); inTvtitle.settextsize (25); -          intColorval=getresources (). GetColor (r.color.white); toTvtitle.settextcolor (Colorval);//Tvtitle.settextcolor (color.white); + tvtitle.setgravity (gravity.center); -           theLayoutparams layoutparams= *                  Newlayoutparams (layoutparams.match_parent, layoutparams.match_parent); $ Actionbar.setcustomview (tvtitle,layoutparams);Panax Notoginseng           -}

Implementation results:

Code setting styles for Android Actionbar and menu

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.