android应用换肤功能的实现

最近公司项目需求要求实现换肤功能,我就一个小菜鸟一个,于是上网各种找资源看各种代码终于实现了

新建BaseActivity

public class BaseActivity extends Activity {
    private int themes;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        themes = new SkinSettingManager(this).getCurrentSkinRes();
        this.setTheme(themes);
        super.onCreate(savedInstanceState);
    }
}

1在atrrs定义定义背景和文字颜色属性

<resources>
    <attr name="mainbackground" format="reference|color"/>
    <attr name="settingbackground" format="reference|color"/>
    <attr name ="backg" format="reference|color"/>
</resources>

2在color中定义颜色属性如

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="white">#ffffff</color>
    <color name="night">#000000</color>
    <color name="red">#fff000</color>
    <color name="yellow">#000fff</color>
</resources>

3在Styles中定义不同皮肤的theme名称及样式如

 <style name="normalTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="mainbackground">@color/white</item>
    <item name="settingbackground">@color/white</item>
    <item name="android:textColor">@color/night</item>
    <item name="backg">@color/red</item> 
</style>
<style name="nightTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="mainbackground">@color/night</item>
    <item name="settingbackground">@color/night</item>
    <item name="android:textColor">@color/white</item>
    <item name="backg">@color/yellow</item> 
      </style>

在所有activity都要继承BaseActivity

在视图文件xml中也要设置背景(ndroid:background="?settingbackground"


<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:background="?settingbackground"
    tools:context="com.example.administrator.myapplication.TwoActivity">

</RelativeLayout>

创建修改Theme类SkinSettingManager


/**
 * 改变Theme
 */
public class SkinSettingManager {
    private final static String SKIN_PREF = "skinSetting";
    private SharedPreferences skinSettingPreference;
    private String key = "skin_type";
    private SharedPreferences.Editor editor;
    private int[] skinResources = { R.style.normalTheme, R.style.nightTheme };
    private Activity mActivity;
    public SkinSettingManager(Activity activity) {
        this.mActivity = activity;
        skinSettingPreference = mActivity.getSharedPreferences(SKIN_PREF, 0x0001);
    }
    /**
     * 获取当前程序的皮肤序号
     */
    public int getSkinType() {
        return skinSettingPreference.getInt(key, 0);
    }
    /**
     * 把皮肤序号写到全局设置里去
     */
    public void setSkinType(int j) {
        editor = skinSettingPreference.edit();
        editor.putInt(key, j);
        editor.commit();
    }
    /**
     * 获取当前皮肤的style
     * @return
     */
    public int getCurrentSkinRes() {
        int getSkinLen = getSkinType();
        return skinResources[getSkinLen];
    }
    /**
     * 用于切换皮肤
     */
    public int toggleSkins() {
        int skinType = getSkinType();
        if (skinType == 0) {
            skinType = 1;
        } else {
            skinType = 0;
        }
        setSkinType(skinType);
        return getCurrentSkinRes();
    }
}
下面就是实现的效果图了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值