使用开源框架——AndroidChangeSkin,实现app换肤

AndroidChangeSkin鸿洋开源的一个换肤框架,喜欢的可以去Star下。

简介

AndroidChangeSkin是一种完全无侵入的换肤方式,支持插件式和应用内,无需重启Activity。

使用换肤效果图

默认:

换肤:

配置环境:

1、在app下的build.gradle添加compile 'com.zhy:changeskin:4.0.2'依赖即可。

2、或者下载changeskin,作为module依赖至主项目,例如:

dependencies { compile project(':changeskin') }
复制代码

使用:

1、在Application初始化

public class MyApplication extends Application
{
	private static Context mContext;

	@Override
	public void onCreate()
	{
		super.onCreate();
		mContext = getApplicationContext();
		SkinManager.getInstance().init(this);
	}

	public static Context getContext()
	{
		return mContext;
	}
}
复制代码

2、在attrs.xml定义资源:

    <!-- changeskin 默认 -->
    <color name="menu_item_text_color">#ffffffff</color>
    <color name="item_text_color">#ff000000</color>
    <color name="main_bg">#fffffffe</color>

    <!--应用内换肤资源-->
    <color name="item_text_color_red">#ff0000</color>
    <color name="menu_item_text_color_red">#ff0000</color>
    <color name="main_bg_red">#ff0000</color>

    <color name="item_text_color_yellow">#E8BF6A</color>
    <color name="menu_item_text_color_yellow">#E8BF6A</color>
    <color name="main_bg_yellow">#E8BF6A</color>

    <color name="item_text_color_green">#00ff00</color>
    <color name="menu_item_text_color_green">#00ff00</color>
    <color name="main_bg_green">#00ff00</color>
复制代码

3、activity_main.xml使用时添加 android:tag

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:tag="skin:main_bg:background"
                tools:context="com.lwj.mytestpro.MainActivity">
    <!--android:tag="skin:main_bg:background"-->

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/button13"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:tag="skin:item_text_color:textColor"
                android:text="换肤"/>
            <Button
                android:id="@+id/button14"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:tag="skin:item_text_color:textColor"
                android:text="恢复默认肤色"/>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

复制代码

GitHub中该项目对tag的使用介绍如下:

tag属性分为3部分组成:

skin
资源的名称,即插件包中资源的名称,需要与当前app内使用的资源名称一致。
支持的属性,目前支持src,background,textColor,支持扩展。
3部分,必须以:分隔拼接。

对于一个View多个属性需要换肤的,android:tag="skin:item_text_color:textColor|skin:icon:src" 同样使用|进行分隔。

简言之:如果你哪个View需要换肤,就添加tag属性,tag值按照上述方式设置即可。
复制代码

4、应用内换肤

MainActivity:

public class MainActivity extends AppCompatActivity
{

	private Button skin;
	private Button defultSkin;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		SkinManager.getInstance().register(this);
		setContentView(R.layout.activity_main);
		Utils.init(this);
		initView();
	}

	private void initView()
	{

		//换肤
		skin = (Button)this.findViewById(R.id.button13);
		skin.setOnClickListener(mOnClickListener);

		defultSkin = (Button)this.findViewById(R.id.button14);
		defultSkin.setOnClickListener(mOnClickListener);
	}

	private View.OnClickListener mOnClickListener = new View.OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			switch(view.getId()){

				case R.id.button13:
					SkinManager.getInstance().changeSkin("yellow");
					break;
				case R.id.button14:
					SkinManager.getInstance().removeAnySkin();
					break;
				default:
					break;
			}
		}
	};

	@Override
	protected void onDestroy()
	{
		Toast.makeText(this, "MainActivity.onDestroy", Toast.LENGTH_SHORT).show();
		SkinManager.getInstance().unregister(this);
		super.onDestroy();
	}
}
复制代码

首先要在onCreate调用register注册,在onDestroy调用unregister解注册。使用SkinManager.getInstance().changeSkin("yellow")即可完成换肤。

注意:这里的changeSkin传入的是字符串yellow,跟我在attrs.xml文件定义属性名字的后缀一致。

<color name="menu_item_text_color">#ffffffff</color>
<color name="item_text_color">#ff000000</color>
<color name="main_bg">#fffffffe</color>

<color name="item_text_color_yellow">#E8BF6A</color>
<color name="menu_item_text_color_yellow">#E8BF6A</color>
<color name="main_bg_yellow">#E8BF6A</color>
复制代码

关于AndroidChangeSkin的使用,还可以参考这篇文章Android 夜间模式changeskin小结,这篇文章的好处是:对踩过的坑总结得不错。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值