写在前头
Android的换肤机制有不少,通过加载不同资源文件进行换肤,通过不同的Style文件进行换肤,但是最主流的还是插件式换肤,将资源文件打成一个包,通过AssetManager去加载这个包中的资源文件来换肤。
换肤代码
public class SkinManager {
private SkinManager(){
}
private static SkinManager mInstance;
public Resources getmResource() {
return mResource;
}
private Resources mResource;
private String mSkinName;
private String mSkinPath;
private static Context mContext;
private String mPackageName;
private boolean IsThemeExists = false;
public static SkinManager getInstance(Context context){
if(mInstance==null){
mInstance = new SkinManager();
mContext = context;
}
return mInstance;
}
public void initSkinName(String path){
this.mSkinName = path;
mSkinPath = mContext.getFilesDir()+ "/Skins/"+ mSkinName;
File file = new File(mSkinPath);
if(TextUtils.isEmpty(mSkinName)||!file.exists()){
mResource = mContext.getResources(); //使用默认资源
mPackageName = mContext.getPackageName();
return;
}
try {
//读取本地皮肤资源
Resources superRes = mContext.getResources();
//通过反射创建AssetManger
AssetMan