前言
之前做个APP需要用到换肤,在githup上面找了很久,终于找到一款功能强大、基本能够满足产品需求的换肤框架,那就是Android-skin-support,这个框架换肤功能很强大、不管是白天、黑夜还是任意颜色、图片的皮肤包都可以,比较方便。
正文
Android-skin-support的githup地址
导入依赖包
// FlycoTabLayout 控件换肤支持
compile 'skin.support:flycotablayout:2.1.2'
compile 'skin.support:skin-support:2.1.3'
// skin-support 基础控件支持
compile 'skin.support:skin-support-design:1.2.5'
// skin-support-design material design 控件支持[可选]
compile 'skin.support:skin-support-constraint-layout:1.0.2'
1. 在Application的oncreate中初始化
@Override
public void onCreate() {
super.onCreate();
initSkinSupport(this);
}
/**
* 初始化换肤控件
*/
private void initSkinSupport(Application application) {
SkinCompatManager.withoutActivity(application)
.addInflater(new SkinMaterialViewInflater()) // material design
.addInflater(new SkinConstraintViewInflater()) // ConstraintLayout
.addInflater(new SkinFlycoTabLayoutInflater()) // H07000223/FlycoTabLayout
.setSkinStatusBarColorEnable(false)
.loadSkin();
}
2. 加入换肤包
将换肤包放在src/main/assets/skins的文件夹下面,将皮肤包的名称的后缀由.apk改成.skin.
3. 开始换肤
点击换肤按钮开始换肤
在代码中切换本地的几个主题,这几个主题都是放在本地的,也就是一直存放在src/main/assets/skins文件件下面的,不需要从网上下载。
/**
* 本地颜色主题变化
*/
private void initYsZhuti(String themeId) {
switch (themeId) {
case "1":
changeSkinApk("default", "1");
break;
case "2":
CustomViewSkinUtils.copySkinApk(this, "2");
changeSkinApk("2.skin", "2");
break;
case "3":
CustomViewSkinUtils.copySkinApk(this, "3");
changeSkinApk("3.skin", "3");
break;
case "4":
CustomViewSkinUtils.copySkinApk(this, "4");
changeSkinApk("4.skin", "4");
break;
}
}
开始切换主题的相关视图,default是默认主题
private void changeSkinApk(final String highZhuTiName, String themeId) {
rl_use_zhuti.setEnabled(false);
if (highZhuTiName == null)
return;
if ("default".equals(highZhuTiName)) {
skinCompatManager.restoreDefaultTheme();
spUtils.saveMyZhuTi(themeBeanId);
tv_use_zhuti.setBackgroundResource(R.drawable.layer_zhuti_preview_press_tv);
tv_use_zhuti.setText(getResources().getString(R.string.wo_have_use));
tv_use_zhuti.setTextColor(ContextCompat.getColor(mActivity, R.color.preview_b_press_ft));
rl_use_zhuti.setEnabled(false);
} else {
loadDialog.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
skinCompatManager.loadSkin(highZhuTiName, new SkinCompatManager.SkinLoaderListener() {
@Override
public void onStart() {
}
@Override
public void onSuccess() {
loadDialog.dismiss();
spUtils.saveMyZhuTi(themeBeanId);
tv_use_zhuti.setBackgroundResource(R.drawable.layer_zhuti_preview_press_tv);
tv_use_zhuti.setText(getResources().getString(R.string.wo_have_use));
tv_use_zhuti.setTextColor(ContextCompat.getColor(mActivity, R.color.preview_b_press_ft));
}
@Override
public void onFailed(String s) {
loadDialog.dismiss();
rl_use_zhuti.setEnabled(true);
}
});
}
},700);
}
其中换肤的两个关键方法
// 指定皮肤插件, 并且监听加载状态
SkinCompatManager.getInstance().loadSkin("new.skin");
// 恢复应用默认皮肤
SkinCompatManager.getInstance().restoreDefaultTheme();