android 使用Theme + attr换肤
2015-07-22 17:14
435 查看
项目有支持夜间模式的需求,开始从theme一点点摸索。据了解。很多人采用的方式是更换主题后发送通知,所有界面在接受到通知后,重新set一遍控件。这样的好处是不需要重启界面,但是代码写起来比较繁琐,且代码量很大。
首先从Theme + attr结合的方式开始,比如说在attr.xml里可以定义如下属性:
然后在styles.xml里定义主题:
在Activity中设置的方法如下,在onCreate函数里根据当前主题设置:
在fragment里设置主题时,在onCreateView里进行如下设置:
个人理解,这两种方法本质上都是将theme的内容添加到当前的上下文信息context中进行加载。
在代码中可以使用如下方法,使用attr属性设置颜色。。再也不用if else啦。。
首先从Theme + attr结合的方式开始,比如说在attr.xml里可以定义如下属性:
<attr name="btnColor" format="color" /> <attr name="mainBackground" format="color" /> <attr name="mainTextColor" format="reference|color" /> <attr name="textString" format="string" /> <attr name="textStyle" format="reference"></attr> <declare-styleable name="EditTextExt"> <attr name="TextColor" format="reference|string"></attr> </declare-styleable><span style="font-family: Arial, Helvetica, sans-serif;"> </span>
然后在styles.xml里定义主题:
<!-- 默认 --> <style name="MyThemeDefault" parent="@android:style/Theme"> <item name="btnColor">#00ff00</item> <item name="mainBackground">#ffffff</item> <item name="mainTextColor">#367895</item> <item name="textString">默认主题</item> <item name="textStyle">@style/textStyle.TMyTextStyle</item> </style> <!-- 夜间 --> <style name="MyThemeNight" parent="@android:style/Theme"> <item name="btnColor">#0000ff</item> <item name="mainBackground">#000000</item> <item name="mainTextColor">#555555</item> <item name="textString">夜间主题</item> <item name="textStyle">@style/textStyleNight.TMyTextStyle</item> </style>我们可以在theme里定义好某个属性的颜色,然后在具体的style中使用:
<style name="TextColorSection"> <item name="android:textColor">?attr/mainTextColor</item> </style>设置主题有两种途径,一种是在activity里设置,一种是在fragment里设置。
在Activity中设置的方法如下,在onCreate函数里根据当前主题设置:
setTheme(R.style.MyThemeNight);在inflate布局时要注意,使用当前activity的Context加载布局,如mContext.getLayoutInflater.inflate.不然在加载布局时会找不到设置的属性值而崩溃。
在fragment里设置主题时,在onCreateView里进行如下设置:
Context ctxWithTheme; ctxWithTheme = new ContextThemeWrapper(getActivity().getApplicationContext(), R.style.MyThemeNight); mInflater = inflater.cloneInContext(ctxWithTheme);在加载布局时使用创建的mInflater进行加载。
个人理解,这两种方法本质上都是将theme的内容添加到当前的上下文信息context中进行加载。
在代码中可以使用如下方法,使用attr属性设置颜色。。再也不用if else啦。。
<pre name="code" class="java">TypedValue typedValue = new TypedValue(); MainActivity.getInstance().getTheme().resolveAttribute(R.style.TextColorItemTitle, typedValue, true); int[] attribute = new int[] { R.attr.text_cl_title }; TypedArray array = MainActivity.getInstance().obtainStyledAttributes(typedValue.resourceId, attribute); int textColor = array.getColor(0, R.color.kw_commen_color_white); tabView.setTextColor(textColor); array.recycle();
相关文章推荐
- android闹钟(五):实现闹钟功能
- Android——coredump解析
- 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(二)Android客户端功能展示
- Android的NDK开发(2)——利用Android NDK编写一个简单的HelloWorld
- Android的NDK开发(2)——利用Android NDK编写一个简单的HelloWorld
- android闹钟(四):实现计时器
- Android的NDK开发(1)————Android JNI简介与调用流程
- Android的NDK开发(1)————Android JNI简介与调用流程
- 在AndroidStudio中创建java应用
- android闹钟(三):实现时钟功能
- android闹钟(二):整体布局和MainActivity
- android 卡片式视图组件 cardview的使用
- Android动画学习笔记-Android Animation
- android 模拟器模拟非root权限
- android闹钟(一):运行效果
- android解析,组包XML文件
- Android Studio关联SVN进行版本控制
- android wifi 相关方面整理
- Android官方技术文档翻译——Eclilpse项目迁移
- Android 如何在进入有scrollView的页面时滑动条置顶