In the recent project to implement the function of switching theme, the following is how I implemented this function in the project, generally we need to change the background color of the entire application can be set GetWindow (). Getdecorview (). SetBackgroundColor (int color). But this is obviously not what we want, and there are a lot of bad places. Why is it? First, it will change the background color of the entire page. Second, the layout of the background must also be transparent to be effective. So this way sometimes is not enough to meet our needs. Below I will introduce my solution for you, below is an implementation.
Click on the Settings app theme color button above, then jump to the theme Color selection screen, select the appropriate theme color in the interface, you can find the interface title layout will immediately follow the selected theme color to switch. I'll post the corresponding code snippet below.
1. Define an interface for monitoring color changes
public interface Oncolorchangedlistener {void oncolorchanged (int color);}
2. Create a theme Color management class
Import Java.util.arraylist;import Java.util.list;import Android.content.context;import Com.qiulong.changedmotivecolortest.r;import Com.qiulong.changedmotivecolortest.activity.myapplication;public Class Colormanager {public static final int default_color = 0xff0c89d8;private final list<oncolorchangedlistener> l Isteners = new arraylist<oncolorchangedlistener> ();p rivate int mcurrentcolor = default_color;private static Colormanager instance;public static Colormanager getinstance () {if (instance = = null) {Return instance = new Colormanager ( );} return instance;} public void AddListener (Oncolorchangedlistener listener) {if (!this.listeners.contains (listener)) {if (listener! = NULL {listener.oncolorchanged (Mcurrentcolor); This.listeners.add (listener);}} public void RemoveListener (Oncolorchangedlistener listener) {this.listeners.remove (listener);} public void notifycolorchanged (int color) {if (Mcurrentcolor = = color) {return;} Mcurrentcolor = color;for (Oncolorchangedlistener listener: This.listeners) {if (listener! = null) {listener.oncolorchanged (color);}}} Public int[] Getskincolor (context context) {return context.getresources (). Getintarray (R.array.default_color_array);} public void Setskincolor (context context, int position) {int[] Colorarr = Context.getresources (). Getintarray ( R.array.default_color_array); MyApplication.mPreference.setSkinColorValue (Colorarr[position]); notifycolorchanged (Colorarr[position]);}}
3. Customize a linearlayout rewrite Onattachedtowindow and Ondetachedfromwindow method, and the oncolorchanged (int color) method that implements the Oncolorchangedlistener listener interface, of course, you can also inherit other view.
Import Com.qiulong.changedmotivecolortest.mode.colormanager;import Com.qiulong.changedmotivecolortest.mode.oncolorchangedlistener;import Android.content.context;import Android.graphics.lightingcolorfilter;import Android.graphics.drawable.colordrawable;import Android.util.attributeset;import Android.widget.linearlayout;public class Colorlinearlayout extends LinearLayout Implementsoncolorchangedlistener {public Colorlinearlayout (context context, AttributeSet Attrs,int defstyleattr) { Super (context, attrs, defstyleattr);//TODO auto-generated Constructor Stub}public colorlinearlayout (context context, AttributeSet attrs) {Super (context, attrs);//TODO auto-generated Constructor stub}public colorlinearlayout (context Context) {super (context);//TODO auto-generated constructor stub} @Overridepublic void oncolorchanged (int color) {if ( Getbackground ()! = null&&! (Getbackground () instanceof colordrawable)) {Getbackground (). Setcolorfilter (New Lightingcolorfilter (color, 1));} else {SETBACKGROundcolor (color);}} @Overrideprotected void Onattachedtowindow () {colormanager.getinstance (). AddListener (this); Super.onattachedtowindow ();} @Overrideprotected void Ondetachedfromwindow () {colormanager.getinstance (). RemoveListener (this); Super.ondetachedfromwindow ();}}
4. Theme Color selection activity
Import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Com.qiulong.changedmotivecolortest.r;import Com.qiulong.changedmotivecolortest.mode.colormanager;public class Motiveactivity extends Activity {private final int[ ] layouts = {r.id.skin_01, r.id.skin_02, r.id.skin_03,r.id.skin_04, r.id.skin_05}; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_motive); InitView ();} private void Initview () {Findviewbyid (r.id.motive_back). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {finish ();}}); int colorarr[] = colormanager.getinstance (). Getskincolor (this), for (int i = 0; i < layouts.length; i++) {View view = Fi Ndviewbyid (Layouts[i]); View color = View.findviewbyid (R.id.motive_item_color); View selected = View.findviewbyid (r.id.motive_item_selected); Color.setbackgroundcolor (Colorarr[i]); if (ColorArr[i] = = MyApplication. Mpreference.getskincolorvalue ()) {selected.setvisibility (view.visible);} Color.setonclicklistener (New Onskincolorclicklistener (i));}} Class Onskincolorclicklistener implements Onclicklistener {private int position;public onskincolorclicklistener (int Position) {this.position = position;} @Overridepublic void OnClick (view v) {for (int i = 0; i < layouts.length; i++) {View view = Findviewbyid (Layouts[i]); View selected = View.findviewbyid (r.id.motive_item_selected); selected.setvisibility (i = = position?) View.VISIBLE:View.GONE); Colormanager.getinstance (). Setskincolor (Motiveactivity.this,position);}}}
5, finally paste the theme color set Arrays.xml
<!--Apply Theme colors- <integer-array name= "Default_color_array" > <item>0xff0c89d8</item > <item>0xFFB32CD1</item> <item>0xFFF676C1</item> <item> 0xff04ba19</item> <item>0xFF18D9BF</item> </integer-array>
Because of the relationship of time, now only paste the corresponding code, and then do a detailed explanation! In fact, are very simple, do not know the friend can ask questions. The above content is only for reference and learning, if there is something wrong, please correct, thank you.
Download Source Please poke here:http://download.csdn.net/detail/baidu_23478311/9514523
Android implements one-click Switch app theme Colors