android应用换肤功能的实现

来源:互联网 发布:seo方案的步骤 编辑:程序博客网 时间:2024/04/20 04:23

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

新建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();    }}
下面就是实现的效果图了

1 0