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
- android应用换肤功能的实现
- Android应用如何实现换肤功能
- Android应用如何实现换肤功能
- Android应用如何实现换肤功能
- Android应用如何实现换肤功能
- Android应用如何实现换肤功能
- Android应用如何实现换肤功能
- Android应用如何实现换肤功能
- Android--应用如何实现换肤功能
- Android应用如何实现换肤功能
- android 换肤功能的实现
- android换肤功能实现
- Android 实现换肤功能
- Android实现换肤功能
- Android 换肤功能实现
- Android 打造自己的个性化应用(二):应用程序内置资源实现换肤功能
- Android 打造自己的个性化应用(二):应用程序内置资源实现换肤功能
- Android 打造自己的个性化应用(二):应用程序内置资源实现换肤功能
- RxJava 小试
- Java finally语句到底是在return之前还是之后执行?
- eclipse 开发环境的设置
- Mobileye采用单目摄像头做ADAS太不精确
- iOS触摸手势——UITouch
- android应用换肤功能的实现
- C++第10/11周项目 继承与派生 【项目4- 教师兼干部类】(复习新版)
- HTTP 消息结构
- WCF NetTcpBinding.ReaderQuotas.MaxDepth 引用异常故障排除
- Spring (3.2.4) 常用jar 包解析
- IDF实验室 聪明的小羊
- 刷新Tableview的行,刷新tableview的整个区
- 前端 HTML小字体图标
- inline 内联函数的使用