一、前言:
Android-skin-support: 一款 Android 换肤框架, 极低的学习成本, 极好的用户体验.
只需要一行代码, 就可以实现换肤, 你值得拥有!!!
SkinCompatManager.withoutActivity(this).loadSkin();
就这么简单, 你的APK已经拥有了强大的换肤功能, 当然现在是拥有了换肤功能, 别忘了制作皮肤包.
框架地址:https://github.com/ximsfei/Android-skin-support
1、 功能
- 支持布局中用到的资源换肤。
- 支持代码中设置的资源换肤。
- 默认支持大部分基础控件,Material Design换肤。
- 支持动态设置主题颜色值,支持选择sdcard上的图片作为drawable换肤资源。
- 支持多种加载策略(应用内/插件式/自定义sdcard路径/zip等资源等)。
- 资源加载优先级: 动态设置资源-加载策略中的资源-插件式换肤/应用内换肤-应用资源。
- 支持定制化,选择需要的模块加载。
- 支持矢量图(vector/svg)换肤。
- skin-support 4.0.0以上支持AndroidX,4.0.0以下支持support库
2、 展示效果图:
demo地址:https://github.com/lyyRunning/AndroidSkinDemo
二、使用:
1、依赖
implementation 'skin.support:skin-support:4.0.5' // skin-support
implementation 'skin.support:skin-support-appcompat:4.0.5' // skin-support 基础控件支持
implementation 'skin.support:skin-support-design:4.0.5' // skin-support-design material design 控件支持[可选]
implementation 'skin.support:skin-support-cardview:4.0.5' // skin-support-cardview CardView 控件支持[可选]
implementation 'skin.support:skin-support-constraint-layout:4.0.5' // skin-support-constraint-layout ConstraintLayout 控件支持[可选]
2、在Application的onCreate中初始化
@Override
public void onCreate() {
super.onCreate();
SkinCompatManager.withoutActivity(this)
.addInflater(new SkinAppCompatViewInflater()) // 基础控件换肤初始化
.addInflater(new SkinMaterialViewInflater()) // material design 控件换肤初始化[可选]
.addInflater(new SkinConstraintViewInflater()) // ConstraintLayout 控件换肤初始化[可选]
.addInflater(new SkinCardViewInflater()) // CardView v7 控件换肤初始化[可选]
.setSkinStatusBarColorEnable(false) // 关闭状态栏换肤,默认打开[可选]
.setSkinWindowBackgroundEnable(false) // 关闭windowBackground换肤,默认打开[可选]
.loadSkin();
}
3、如果项目中使用的Activity继承自AppCompatActivity,需要重载getDelegate()方法
@NonNull
@Override
public AppCompatDelegate getDelegate() {
return SkinAppCompatDelegateImpl.get(this, this);
}
4、多套资源包
color资源:
#90a4ae
#455a64
#607d8b
#e6e6e6
#EEEDED
注意:1、引入的资源包一定要和res包同一等级 2、不同资源颜色包一定要有后缀_night。
5、build.gradle中引入
android {
...
//引入多套资源包
sourceSets {
main {
res.srcDirs = ['src/main/res', 'src/main/res-red', 'src/main/res-night', 'src/main/res-yellow']
}
}
}
6、切换
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.activity_changeskin_toolbar_img_back:
finish();
break;
case R.id.activity_changeskin_cd_blue:
Toast.makeText(ChangeSkinActivity.this,"切换蓝色",Toast.LENGTH_SHORT).show();
//设置默认颜色
SkinCompatManager.getInstance().restoreDefaultTheme();
break;
case R.id.activity_changeskin_cd_black:
Toast.makeText(ChangeSkinActivity.this,"切换暗黑色",Toast.LENGTH_SHORT).show();
//设置暗黑色
SkinCompatManager.getInstance().loadSkin("night", null, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
break;
case R.id.activity_changeskin_cd_red:
Toast.makeText(ChangeSkinActivity.this,"切换红色",Toast.LENGTH_SHORT).show();
//设置红色
SkinCompatManager.getInstance().loadSkin("red", null, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
break;
case R.id.activity_changeskin_cd_yellow:
Toast.makeText(ChangeSkinActivity.this,"切换黄色",Toast.LENGTH_SHORT).show();
//设置黄色
SkinCompatManager.getInstance().loadSkin("yellow", null, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
break;
default:
break;
}
}
三、代码:
1、MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView activityMainToolbarImgMore;
private DrawerLayout activityMainDrawerlayout;
private LinearLayout menuLeftLayout;
private CardView menuLeftLayoutCdChangeskin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityMainToolbarImgMore = (ImageView) findViewById(R.id.activity_main_toolbar_img_more);
activityMainDrawerlayout = (DrawerLayout) findViewById(R.id.activity_main_drawerlayout);
menuLeftLayout = (LinearLayout) findViewById(R.id.menu_left_layout);
menuLeftLayoutCdChangeskin = (CardView) findViewById(R.id.menu_left_layout_cd_changeskin);
activityMainToolbarImgMore.setOnClickListener(this);
menuLeftLayoutCdChangeskin.setOnClickListener(this);
}
/**
* 一定要加
* @return
*/
@NonNull
@Override
public AppCompatDelegate getDelegate() {
return SkinAppCompatDelegateImpl.get(this, this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.activity_main_toolbar_img_more:
switchLeftMenu();
break;
case R.id.menu_left_layout_cd_changeskin:
startActivity(new Intent(this,ChangeSkinActivity.class));
break;
}
}
private void switchLeftMenu(){
if (activityMainDrawerlayout.isDrawerOpen(menuLeftLayout)) {
activityMainDrawerlayout.closeDrawer(menuLeftLayout);
} else {
activityMainDrawerlayout.openDrawer(menuLeftLayout);
}
}
}
2、activity_main
3、menu_left_layout
4、ChangeSkinActivity
public class ChangeSkinActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView activityChangeskinToolbarImgBack;
private CardView activityChangeskinCdBlue;
private CardView activityChangeskinCdBlack;
private CardView red;
private CardView yellow;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_changeskin);
activityChangeskinToolbarImgBack = (ImageView) findViewById(R.id.activity_changeskin_toolbar_img_back);
activityChangeskinCdBlue = (CardView) findViewById(R.id.activity_changeskin_cd_blue);
activityChangeskinCdBlack = (CardView) findViewById(R.id.activity_changeskin_cd_black);
red = (CardView) findViewById(R.id.activity_changeskin_cd_red);
yellow = (CardView) findViewById(R.id.activity_changeskin_cd_yellow);
activityChangeskinToolbarImgBack.setOnClickListener(this);
activityChangeskinCdBlue.setOnClickListener(this);
activityChangeskinCdBlack.setOnClickListener(this);
red.setOnClickListener(this);
yellow.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.activity_changeskin_toolbar_img_back:
finish();
break;
case R.id.activity_changeskin_cd_blue:
Toast.makeText(ChangeSkinActivity.this,"切换蓝色",Toast.LENGTH_SHORT).show();
//设置默认颜色
SkinCompatManager.getInstance().restoreDefaultTheme();
break;
case R.id.activity_changeskin_cd_black:
Toast.makeText(ChangeSkinActivity.this,"切换暗黑色",Toast.LENGTH_SHORT).show();
//设置暗黑色
SkinCompatManager.getInstance().loadSkin("night", null, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
break;
case R.id.activity_changeskin_cd_red:
Toast.makeText(ChangeSkinActivity.this,"切换红色",Toast.LENGTH_SHORT).show();
//设置红色
SkinCompatManager.getInstance().loadSkin("red", null, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
break;
case R.id.activity_changeskin_cd_yellow:
Toast.makeText(ChangeSkinActivity.this,"切换黄色",Toast.LENGTH_SHORT).show();
//设置黄色
SkinCompatManager.getInstance().loadSkin("yellow", null, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
break;
default:
break;
}
}
/**
* 一定要加
* @return
*/
@NonNull
@Override
public AppCompatDelegate getDelegate() {
return SkinAppCompatDelegateImpl.get(this, this);
}
}
5、activity_changeskin
6、BaseApplication
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SkinCompatManager.withoutActivity(this)
.addInflater(new SkinAppCompatViewInflater()) // 基础控件换肤初始化
.addInflater(new SkinMaterialViewInflater()) // material design 控件换肤初始化[可选]
.addInflater(new SkinConstraintViewInflater()) // ConstraintLayout 控件换肤初始化[可选]
.addInflater(new SkinCardViewInflater()) // CardView v7 控件换肤初始化[可选]
// .setSkinStatusBarColorEnable(false) // 关闭状态栏换肤,默认打开[可选]
// .setSkinWindowBackgroundEnable(false) // 关闭windowBackground换肤,默认打开[可选]
.loadSkin();
}
}
7、color值
#90a4ae
#455a64
#607d8b
#e6e6e6
#EEEDED
8、引入资源包
android {
...
//引入多套资源包
sourceSets {
main {
res.srcDirs = ['src/main/res', 'src/main/res-red', 'src/main/res-night', 'src/main/res-yellow']
}
}
}