At work there is a need to color the icons on the Actionbar when scrolling. After the realization here summarizes the idea.
First, define the style of Actionbar in the theme
<stylename= "Apptheme"Parent= "Basetheme"> <Itemname= "Actionbarstyle">@style/naactionbarstyle</Item> <Itemname= "Actionbarsize">@dimen/actionbar_height</Item>
<Itemname= "Actionbaritembackground">@drawable/action_bar_selector</Item>
<Itemname= "Actionoverflowbuttonstyle">@style/myoverflow.dark</Item> <Itemname= "Homeasupindicator">@drawable/action_bar_icon_back_dark</Item> </style>
Ii. changes in the subregion
2.1 Changing the leftmost return button
Getsupportactionbar (). Sethomeasupindicator (R.drawable.action_bar_icon_back_light);
2.2 Changing the color and transparency of text
This is more complex, first of all to define a class like this:
PackageCom.duitang.main.util;ImportAndroid.graphics.Color;ImportAndroid.os.Parcel;ImportAndroid.support.annotation.NonNull;ImportAndroid.text.TextPaint;ImportAndroid.text.style.ForegroundColorSpan;/*** Created by Ylord on 14-12-16. *@authorYlord *@sincev3.7 * from Cyril mottier*/ Public classAlphaforegroundcolorspanextendsForegroundcolorspan {Private floatMalpha; PublicAlphaforegroundcolorspan (intcolor) { Super(color); } PublicAlphaforegroundcolorspan (Parcel src) {Super(SRC); } @Override Public voidupdatedrawstate (@NonNull textpaint ds) {Ds.setcolor (Getalphacolor ()); } Public floatGetalpha () {returnMalpha; } Public voidSetalpha (floatAlpha) {Malpha=Alpha; } Private intGetalphacolor () {intForegroundcolor =Getforegroundcolor (); returnColor.argb ((int) (Malpha * 255), color.red (Foregroundcolor), Color.green (Foregroundcolor), Color.Blue (Foregroundcolor)); }}
You can then call Actionbar's Settitle method dynamically in your code.
Private Alphaforegroundcolorspan Malphaforegroundcolorspan; Private Spannablestringbuilder Mspannablestringbuilder;
New Alphaforegroundcolorspan (Getresources (). GetColor (R.color.dark));
Private void settitlealpha (float Alpha) { malphaforegroundcolorspan.setalpha (alpha); 0, mspannablestringbuilder.length (), spannable.span_exclusive_exclusive); Getsupportactionbar (). Settitle (Mspannablestringbuilder); }
2.3 Change the rightmost menu icon
MenuItem Moreitem; @Override publicboolean oncreateoptionsmenu (Menu menu) { Null); Moreitem.seticon (R.drawable.action_bar_icon_more); Moreitem.setshowasaction (menuitem.show_as_action_always); return Super . Oncreateoptionsmenu (menu); }
Dynamic change
if NULL ) { Moreitem.seticon (r.drawable.action_bar_icon_more_light);}
2.4 Change the overflow on the far right
If multiple MenuItem are aggregated together, then three dots are displayed. How do you dynamically change the icons for these three dots?
Define the style of the overflow in the style:
<stylename= "Myoverflow.dark"Parent= "@style/widget.appcompat.light.actionbutton.overflow"> <Itemname= "Android:src">@drawable/action_bar_icon_more_dark</Item> <Itemname= "Android:contentdescription">@string/accessibility_overflow</Item></style>
The key point here is to write a contentdescription, as the overflow logo. Then write the following code in the code:
Private Tintimageview overflow;
//The content description used to locate the overflow button FinalString Overflowdesc =getString (R.string.accessibility_overflow); //The top-level window FinalViewGroup Decor =(ViewGroup) GetWindow (). Getdecorview (); //Wait a moment to ensure the overflow button can be locatedDecor.post (NewRunnable () {@Override Public voidrun () {//The List that contains the matching views FinalArraylist<view> outviews =NewArraylist<>(); //Traverse The View-hierarchy and locate the overflow buttonDecor.findviewswithtext (Outviews, Overflowdesc, view.find_views_with_content_description); //Guard against any errors if(Outviews.isempty ()) {return; } //Do something with the viewoverflow = (tintimageview) outviews.get (0); } });
It can then be changed dynamically in the code:
if null) { overflow.setimageresource (r.drawable.action_bar_icon_more_light); }
Reference from: http://stackoverflow.com/questions/22046903/changing-the-android-overflow-menu-icon-programmatically
Dynamically change the menu icon on the Actionbar