Code setting style for Actionbar and menu in Android _android

Source: Internet
Author: User
Tags event listener

Menu part XML code

 <?xml version= "1.0" encoding= "Utf-8"?> "<menu xmlns: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:act ionviewclass= "Android.widget.SearchView" android:showasaction= "Ifroom|collapseactionview" Android:o
    rderincategory= "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:orderincategory= "The Android:showA"
    saction= "Never"/> <item android:id= "@+id/action_font" android:title= "Font size" android:orderincategory= "100" android:showasaction= "Never"/> </menu> 

Menu icon Display Implementation "Rewrite onmenuopened method, use reflection principle" overflower

/**
   * Display overflower menu icon */
  @Override public
  boolean onmenuopened (int featureid, menu menu) { 
    if ( Featureid = = Window.feature_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

 /**
 * Menu click Action Monitor Event
 /@Override public
 boolean onoptionsitemselected (MenuItem item) {
   switch (Item.getitemid ()) {Case
   Android. 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

/** * Initialize actionbar content * * * * private actionbar Actionbar;
    private void Initactionbar () {Actionbar=super.getactionbar ();
    
    Actionbar.show ();
    Displays the home area actionbar.setdisplayshowhomeenabled (true);
    Set the Home Zone fallback button actionbar.setdisplayhomeasupenabled (true);
    
    Actionbar.sethomeasupindicator (R.drawable.back_move_details_normal);
    Do not show home area title actionbar.setdisplayshowtitleenabled (true);//Actionbar.settitle ("News");//Set title//Do not show logo picture 
        Actionbar.setdisplayuselogoenabled (FALSE)/////Remove icon icon for Home area "set icon color to transparent" drawable colordrawable=new Colordrawable (Android.
    R.color.transparent);
    
    Actionbar.seticon (colordrawable);
    Custom Area actionbar.setdisplayshowcustomenabled (true);
    TextView tvtitle=new TextView (this);//this, currently carrying Tvtitle.settext ("news Information");//tvtitle.setid ();
    Tvtitle.settextsize (25);
    int colorval=getresources (). GetColor (R.color.white); Tvtitle.settextcolor (colorval);//tvtitle.settexTColor (Color.White);
    
    Tvtitle.setgravity (Gravity.center); 
    Layoutparams layoutparams= New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent);
    
 Actionbar.setcustomview (Tvtitle,layoutparams);

 }

Implementation effect:

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

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.