自己开发的APP中用到了换肤功能,记录下这个换肤框架。
一种完全无侵入的换肤方式,支持插件式和应用内,无需重启Activity.
1、Android Studio引用依赖库
dependencies { compile 'com.zhy:changeskin:4.0.2' }
2、Application中去调用SkinManager.getInstance().init(this);
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); SkinManager.getInstance().init(this); } }3、在需要换肤的Activity的onCreate和onDestory中,分别:
@Override protected void onCreate(Bundle savedInstanceState) { SkinManager.getInstance().register(this); //... } @Override protected void onDestroy() { super.onDestroy(); SkinManager.getInstance().unregister(this); }4、布局文件中添加支持,主要依赖于tag属性:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:tag="skin:item_text_color:textColor" android:text="@string/hello_world" android:textColor="@color/item_text_color"/>
tag属性分为3部分组成:
- skin
- 资源的名称,即插件包中资源的名称,需要与当前app内使用的资源名称一致。
- 支持的属性,目前支持src,background,textColor,支持扩展。
3部分,必须以:分隔拼接。
对于一个View多个属性需要换肤的,android:tag="skin:item_text_color:textColor|skin:icon:src"
同样使用|进行分隔。
简言之:如果你哪个View需要换肤,就添加tag属性,tag值按照上述方式设置即可。
5、调用换肤方法
SkinManager.getInstance().changeSkin(suffix);
suffix参数传入资源名后缀即可,如资源名为main_bg_red 只需传入red即可。