There are many ways to change the skin, there is a change of background picture (image from the program resource files, sdcard, network download), there is a replacement theme style, there are apk and so on.
Replace the program skins with APK, such a way, convenient, simple, and make the main program APK package smaller.
First download the skin apk package, install, and then make the current program read the new installed APK package resource files, the next time you open the program, still read the last set of APK resources, if the theme apk is uninstalled, then read the default resources.
The core code (see the full code for the project compression package) is as follows:
Main program:
Read the package name of the installed Skin Pack, app icon and app name, and of course the most important real package name, so that it can be loaded by the name of the package to read the resource names of other resources in the current skin package.
Private RunnableSerachskin = new Runnable () {
@Override
Public void Run () {
Packagemanager Manager = context. Getpackagemanager ();
list<packageinfo> packages = Manager.getinstalledpackages (Packagemanager. Permission_granted);
list<packageinfo> skins = new arraylist<packageinfo> ();
for (PackageInfo info:packages) {//traverse the installed Skin Pack
if (info.) packagename. StartsWith ("Com.theme.")) {//corresponding to the package name of the skin pack
Skins.add (info);
}
}
if (Skins.size () > 0) {
Message msg = mhandler. Obtainmessage ();
Msg.obj = skins;
Msg.setdata (new Bundle ());
Msg. What =Message_searched_skin;
Mhandler. SendMessage (msg);
} Else {
Mhandler. Sendemptymessage (message_searched_skin_for_nonthing);
}
}
};
this. Setonthemechangedlistener (new Onthemechangedlistener () {
@Override
Public void onChanged (String themepackagename) {
LOG.I ("","Themepackagename::" + themepackagename);
Try {
Context Themecontext = themesecondactivity. this. Createpackagecontext (Themepackagename, Context. Context_ignore_security);
Resources themeresources = Themecontext.getresources ();
Setcontrolsstyle (Themepackagename, themeresources);
} catch (Namenotfoundexception e) {
E.printstacktrace ();
}
}
});
/**
* Set controls for new skins
* @param The package name of the Themepackagename skin pack
* @param Resources of themeresources Skin pack
*/
Private void Setcontrolsstyle (String themepackagename, Resources themeresources) {
LOG.I ("", "Themepackagename::" + themepackagename);
button. SetBackground (Themeresources.getdrawable (Themeresources.getidentifier ("Button_selector" ,"drawable", Themepackagename)); //Ensure drawable button_selector file in resource file
GridView. SetBackgroundColor (Themeresources.getcolor (Themeresources.getidentifier ("Background_ GridView "," Color ", themepackagename)));
}
Looking for a long time apk skin, similar to Sina Weibo or QQ skin, find the case is simply to change the theme in the current project, or the APK package, but when placed in their own project, throw a null pointer or a resource file does not exist exception.
The reason, because the resource file is not found, in the current project is getdrawable (r.drawable.xx) way to get the picture, This method is based on the r.drawable.xx corresponding to an int value (int value in the project's Gen folder under the class class of R.java) to identify and load, so to ensure that the main project and skin engineering corresponding to the two values are the same, if not the same, there may be dislocation (int value corresponds to other resources) , and of course the possibility of causing a null pointer or not finding the resource file (int value does not exist).
The solution is to ensure that the currently read RES resource is a resource for the skin package, read as follows:
context Themecontext = ThemeSecondActivity.this.createPackageContext (themepackagename,context. context_ignore_security);
Resources themeresources = Themecontext.getresources ();
Project Compression Pack: http://download.csdn.net/detail/xue_wei_love/8581235
Description:apk mode change theme: including multiple page changes, multiple resource changes (can color values, pictures, XML files and other custom resource files). The compression includes 4 projects, described as follows: Skintheme is the main project, skinred is a red theme package, Skingreen is a green theme package, Skinblue is a blue theme package.
Reference:
Http://www.eoeandroid.com/thread-102060-1-1.html
http://bbs.csdn.net/topics/390311059
Http://www.2cto.com/kf/201204/129474.html
http://blog.csdn.net/vincent_czz/article/details/7276118
http://www.javaapk.com/source/417.html#
http://down.51cto.com/data/622207
Android Skin Changer Theme (APK package mode)