The application has built-in resources for skin replacement. A typical application is the QQ space for skin replacement. the application scenario is as follows: the application is generally not big, there are few pages, and the style is relatively simple. Generally, only some resources are implemented or the background is replaced.
Thoughts on the implementation of this skin replacement method:
1. Put several skins in the Res/drawable directory, use sharedpreferences to record the resource ID of the current skin, And then load the activity background when the program starts.
2. the main implementation is in the skin manager skinmanager class. adds the skin resource ID to the collection. this type of scheduling skin replacement, such as initializing the skin, getting the current skin symbol and changing the skin of the corresponding resource.
Next let's take a look:
The implementation of the built-in skin is relatively simple. The following code is directly used:
Androidmainfest. xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tony.skindemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name="com.tony.skindemo.SkinDemoActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
Layout file:
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: textcolor = "# ff00ff" Android: text = "program skin replacement" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <radiogroup Android: id = "@ + ID/skin_options" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <radiobutton Android: layout_weight = "1" Android: id = "@ + ID/radiobutton1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "style 1"/> <radiobutton Android: layout_weight = "1" Android: Id = "@ + ID/radiobutton2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "Style 2"/> <radiobutton Android: layout_weight = "1" Android: Id = "@ + ID/radiobutton3" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Style 3"/> <radiobutton Android: layout_weight = "1" Android: Id = "@ + ID/radiobutton4" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "style 4"/> <radiobutton Android: layout_weight = "1" Android: id = "@ + ID/radiobutton5" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "style 5"/> </radiogroup> </linearlayout>
Main Activity
Package COM. tony. skindemo; import android. app. activity; import android. OS. bundle; import android. view. motionevent; import android. view. window; import android. view. windowmanager; import android. widget. radiobutton; import android. widget. radiogroup; import android. widget. radiogroup. extends; public class skindemoactivity extends activity {private extends msettingmanager; private radiobutton extends; @ overridepublic void oncreate (bundle extension) {super. oncreate (savedinstancestate); // cancel the title bar requestwindowfeature (window. feature_no_title); // complete the full screen display of the form // cancel the getwindow () status bar (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); setcontentview (R. layout. main); // initialize the skin msettingmanager = new skinsettingmanager (this); msettingmanager. initskins (); // set the skin using the single-choice button (you can customize the replacement method, such as the navigation bar, or add the preview function, which is not implemented here) radiobutton1 = (radiobutton) findviewbyid (R. id. radiobutton1); radiobutton2 = (radiobutton) findviewbyid (R. id. radiobutton2); radiobutton3 = (radiobutton) findviewbyid (R. id. radiobutton3); radiobutton4 = (radiobutton) findviewbyid (R. id. radiobutton4); radiobutton5 = (radiobutton) findviewbyid (R. id. radiobutton5); radiogroup = (radiogroup) findviewbyid (R. id. skin_options); radiogroup. setoncheckedchangelistener (New oncheckedchangelistener () {@ overridepublic void oncheckedchanged (radiogroup group, int checkedid) {Switch (checkedid) {case R. id. radiobutton1: msettingmanager. changeskin (1); break; case R. id. radiobutton2: msettingmanager. changeskin (2); break; case R. id. radiobutton3: msettingmanager. changeskin (3); break; case R. id. radiobutton4: msettingmanager. changeskin (4); break; case R. id. radiobutton5: msettingmanager. changeskin (5); break; default: Break ;}}) ;}// for simple implementation, public Boolean ontouchevent (motionevent event) {msettingmanager is implemented. toggleskins (); return Super. ontouchevent (event );}}
Skin Manager:
Package COM. tony. skindemo; import android. app. activity; import android. content. sharedpreferences;/*** skin manager * @ author Tony **/public class skinsettingmanager {public final static string skin_pref = "skinsetting"; Public sharedpreferences skinsettingpreference; private int [] skinresources = {R. drawable. default_wallpaper, R. drawable. wallpaper_c, R. drawable. wallpaper_d, R. drawable. wallpaper_f, R. drawable. wallpaper_g}; private activity mactivity; Public skinsettingmanager (activity) {This. mactivity = activity; skinsettingpreference = mactivity. getsharedpreferences (skin_pref, 3);}/*** get the skin Number of the current program ** @ return */Public int getskintype () {string key = "skin_type"; return skinsettingpreference. getint (Key, 0);}/*** write the skin number to the global settings. ** @ Param J */Public void setskintype (Int J) {sharedpreferences. editor editor = skinsettingpreference. edit (); string key = "skin_type"; Editor. putint (Key, J); Editor. commit ();}/*** obtain the background image resource ID of the current skin ** @ return */Public int getcurrentskinres () {int skinlen = skinresources. length; int getskinlen = getskintype (); If (getskinlen >=skinlen) {getskinlen = 0;} return skinresources [getskinlen];} public void toggleskins () {int skintype = getskintype (); If (skintype = skinresources. length-1) {skintype = 0;} else {skintype ++;} setskintype (skintype); mactivity. getwindow (). setbackgrounddrawable (null); try {mactivity. getwindow (). setbackgrounddrawableresource (getcurrentskinres ();} catch (throwable e) {e. printstacktrace () ;}/ *** used to initialize the skin */Public void initskins () {mactivity. getwindow (). setbackgrounddrawableresource (getcurrentskinres ();}/*** then switch to a background skin */Public void changeskin (int id) {setskintype (ID); mactivity. getwindow (). setbackgrounddrawable (null); try {mactivity. getwindow (). setbackgrounddrawableresource (getcurrentskinres ();} catch (throwable e) {e. printstacktrace ();}}}
In this way, the basic functions of the embedded skin are completed.
If you want to implement it in your own application, you still need to pay attention to the following points (the implementation is not complex, and the specific implementation is not described here ):
1. Implement skin replacement for multiple activities. Use the custom myapplication class to inherit from the application and add the set attributes of the activity. This class is used to store all the activities of the application.
Modify skinmanager. When skin is changed, retrieve the set from the application, traverse the set, and change the skin.
2. You can optimize the user experience, enter the skin replacement interface through the navigation bar, and add the preview function. After you confirm to modify the configuration, the skin replacement function is completed.
3. Add style. Theme and other resources to achieve more complex skin replacement.