Android 切换主题 (二)
Android 切换主题 (二)
背景
我原来写过一篇文章关于 android 切换主题的文章 -- Android 切换主题以及换肤的实现 , 里面介绍了如何使用 setTheme() 来切换主题,只不过使用这个函数有个缺点那就是你必须得重新启动当前的 Activity 才能生效!那么问题来了,有没有方法保证我们既使用了 setTheme() 又能不重启当前的 Activity 呢?我告诉这是有的,下面我就是详细介绍这个方法 (参考 github 上的一个开源项目进行介绍!文末给出这个项目的地址)。
===
原理
众所周知,setTheme() 之后需要重启当前的 Activity 的原因是:它要重新渲染当前的 当前的 ViewTree 。所以现在我们的做法就是我们来自己给他渲染不就行了!那样的话,就不需要重启当前的 Activity 了!下面我们就来看代码吧!
===
代码
代码的实现核心就是:在用户调用 setTheme() 之后,我们获取当前的 Theme ,之后我们在从中获取到我们用的属性,之后在设置到对应的控件上,这样就ok了!
第一步
我们先定义个接口:
public interface ColorUiInterface {
public View getView();
public void setTheme(Resources.Theme themeId);
}
这样的话我们就可以重写所有的控件,让他继承该接口,并实现对应的函数,那么在 setTheme() 之后就可以直接调用每个控件都有的 setTheme() 方法了!
第二步
现在我们就来实现一个自定义的view
public class ColorTextView extends TextView implements ColorUiInterface {
private int attr_drawable = -1;
private int attr_textAppearance = -1;
private int attr_textColor = -1;
public ColorTextView(Context context) {
super(context);
}
public ColorTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.attr_drawable = ViewAttributeUtil.getBackgroundAttibute(attrs);
this.attr_textColor = ViewAttributeUtil.getTextColorAttribute(attrs);
}
public ColorTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.attr_drawable = ViewAttributeUtil.getBackgroundAttibute(attrs);
this.attr_textColor = ViewAttributeUtil.getTextColorAttribute(attrs);
}
@Override
public View getView() {
return this;
}
@Override
public void setTheme(Resources.Theme themeId) {
if (attr_drawable != -1) {
ViewAttributeUtil.applyBackgroundDrawable(this, themeId, attr_drawable);
}
if (attr_textColor != -1) {
ViewAttributeUtil.applyTextColor(this, themeId, attr_textColor);
}
}
}
从以上代码中我们可以看到,我首先获取了一下一些常用的需要换肤的要素,比如:背景色,字体颜色,当让还有其他的,这个随用户定制!当然读者也可以实现其他的自定义 view 。
第三步
现在我们来看下 ViewAttributeUtil 这个类的具体实现!
public class ViewAttributeUtil {
public static int getAttributeValue(AttributeSet attr, int paramInt) {
int value = -1;
int count = attr.getAttributeCount();
for(int i = 0; i <count;i++) {
if(attr.getAttributeNameResource(i) == paramInt) {
String str = attr.getAttributeValue(i);
if(null != str && str.startsWith("?")) {
value = Integer.valueOf(str.substring(1,str.length())).intValue();
return value;
}
}
}
return value;
}
public static int getBackgroundAttibute(AttributeSet attr) {
return getAttributeValue(attr , android.R.attr.background);
}
public static int getCheckMarkAttribute(AttributeSet attr) {
return getAttributeValue(attr, android.R.attr.checkMark);
}
public static int getSrcAttribute(AttributeSet attr) {
return getAttributeValue(attr, android.R.attr.src);
}
public static int getTextApperanceAttribute(AttributeSet attr) {
return getAttributeValue(attr, android.R.attr.textAppearance);
}
public static int getDividerAttribute(AttributeSet attr) {
return getAttributeValue(attr, android.R.attr.divider);
}
public static int getTextColorAttribute(AttributeSet attr) {
return getAttributeValue(attr, android.R.attr.textColor);
}
public static void applyBackgroundDrawable(ColorUiInterface ci, Resources.Theme theme, int paramInt) {
TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
Drawable drawable = ta.getDrawable(0);
if(null != ci) {
(ci.getView()).setBackgroundDrawable(drawable);
}
ta.recycle();
}
public static void applyImageDrawable(ColorUiInterface ci, Resources.Theme theme, int paramInt) {
TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
Drawable drawable = ta.getDrawable(0);
if(null != ci && ci instanceof ImageView) {
((ImageView)ci.getView()).setImageDrawable(drawable);
}
ta.recycle();
}
public static void applyTextAppearance(ColorUiInterface ci, Resources.Theme theme, int paramInt) {
TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
int resourceId = ta.getResourceId(0,0);
if(null != ci && ci instanceof TextView) {
((TextView)ci.getView()).setTextAppearance(ci.getView().getContext(), resourceId);
}
ta.recycle();
}
public static void applyTextColor(ColorUiInterface ci, Resources.Theme theme, int paramInt) {
TypedArray ta = theme.obtainStyledAttributes(new int[]{paramInt});
int resourceId = ta.getColor(0,0);
if(null != ci && ci instanceof TextView) {
((TextView)ci.getView()).setTextColor(resourceId);
}
ta.recycle();
}
}
这个类比较简单,就是根据对应的 themeid 得到对应 themeid 的值!好了目前为止,我们还差一步就是,当我们调用了 Activity 的 setTheme() 方法之后,重新设置到对应的view就行了!
第四步
直接上代码
public class ColorUiUtil {
/**
* 切换应用主题
*
* @param rootView
*/
public static void changeTheme(View rootView, Resources.Theme theme) {
if (rootView instanceof ColorUiInterface) {
((ColorUiInterface) rootView).setTheme(theme);
if (rootView instanceof ViewGroup) {
int count = ((ViewGroup) rootView).getChildCount();
for (int i = 0; i < count; i++) {
changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
}
}
if (rootView instanceof AbsListView) {
try {
Field localField = AbsListView.class.getDeclaredField("mRecycler");
localField.setAccessible(true);
Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear", new Class[0]);
localMethod.setAccessible(true);
localMethod.invoke(localField.get(rootView), new Object[0]);
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
} catch (NoSuchMethodException e3) {
e3.printStackTrace();
} catch (IllegalAccessException e4) {
e4.printStackTrace();
} catch (InvocationTargetException e5) {
e5.printStackTrace();
}
}
} else {
if (rootView instanceof ViewGroup) {
int count = ((ViewGroup) rootView).getChildCount();
for (int i = 0; i < count; i++) {
changeTheme(((ViewGroup) rootView).getChildAt(i), theme);
}
}
if (rootView instanceof AbsListView) {
try {
Field localField = AbsListView.class.getDeclaredField("mRecycler");
localField.setAccessible(true);
Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear", new Class[0]);
localMethod.setAccessible(true);
localMethod.invoke(localField.get(rootView), new Object[0]);
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
} catch (NoSuchMethodException e3) {
e3.printStackTrace();
} catch (IllegalAccessException e4) {
e4.printStackTrace();
} catch (InvocationTargetException e5) {
e5.printStackTrace();
}
}
}
}
}
代码很简单,就是用了递归,从当前的 ViewTree 中找到 继承了我们在第一步定义的那个接口,之后再调用其 setTheme() 方法就行了!!
最后一步
那么我们现在来看下如何在 Activity 中是如何调用的!
setTheme(R.style.theme_1);
ColorUiUtil.changeTheme(rootView, getTheme());
是不是很简单呀!
总结
这个方法我感觉蛮好的,要说唯一的缺点吧!就是使用了递归!有点耗性能!但是这个我估计是可以忽略不计的!!
*** 现在给出这个开源项目的地址 MultipleTheme ***
Android 切换主题 (二)的更多相关文章
- Android 切换主题以及换肤的实现
Android 切换主题以及换肤的实现 一.介绍 现在市面上有很多 APP 有切换主题和皮肤的功能!特别是阅读类的 APP! 上面两张图分别是 知乎 APP 和Fuubo APP的两张截图!都带有切换 ...
- Android 切换主题换肤实现
思路以及实现 1.主题的切换以及实现 首先我们先来明确个概念,现在我所说的切换主题,就切换整个app的颜色风格,当然也有少部分的图片的切换.注意哦 我这边说的是少部分图片哦!如果是大面积的换图片的吧! ...
- Android应用主题与横竖屏的切换
很多App,现在都具有了横竖屏切换的功能,或者说"白天"和"黑夜"主题的切换. 实现起来也非常简单.主要需要注意的是,在切换的同时,页面的数据不能丢失,不然给用 ...
- Android 实现切换主题皮肤功能(类似于众多app中的 夜间模式,主题包等)
首先来个最简单的一键切换主题功能,就做个白天和晚上的主题好了. 先看我们的styles文件: <resources> <!-- Base application theme. --& ...
- Android Wear和二维码
这是一篇发布在Android官方开发者社区博客,15年年初的时候就看到了这篇文章,直到现在才有时间把它翻译下来. 这是一篇如何在Android Wear上面如何正确地展示二维码的文章,里面有许多的经验 ...
- 【Android Studio安装部署系列】八、Android Studio主题皮肤更换
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 Android Studio具有自己的主题皮肤,但是如果想要更换自己喜欢的主题皮肤,可以参考下面的步骤. 注意,更换主题皮肤,之前的 ...
- Android JNI学习(二)——实战JNI之“hello world”
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- Android开发 QRCode二维码开发第三方框架
前言 Android开发里二维码开发经常用到,这里简单的介绍下Android开发里的二维码. 最广泛使用的二维码库zxing zxing是最广泛的二维码库各个平台都可以适用它,但是Android平台使 ...
- Android Studio(十二):打包多个发布渠道的apk文件
Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...
随机推荐
- MySQL UUID函数的详解(转)
MySQL UUID函数的详解 MySQL中可以有二类用于生成唯一值性质的工具:UUID()函数和自增序列,那么二者有何区别呢?我们就此对比下各自的特性及异同点: l 都可以实现生成唯一值的功能: ...
- java调试打断点和不打断点执行结果不一致问题解决
java程序在调试的时候需要debug来跟踪一下结果,有一种情况是这样的,正常执行java程序就会出现问题,而断点debug跟踪此方法的时候却是正常的,不断测试结果都是这样,由此判断有可能是因为此方法 ...
- Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)
描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...
- Docker背景介绍
背景介绍 Docker是PasS提供商DoctCloud开源的一个基于LXC的高级容器引擎,源代码托管在Github上,基于go语言并遵从Apache2.0协议开源.Docker近期非常火热,无论是从 ...
- 连接SQLServer的增删改查方法代码
在Visual C++中用ADO进行数据库编程 1. 生成应用程序框架并初始化OLE/COM库环境 创建一个标准的MFC AppWizard(exe)应用程序CADOConnection,然后在使用A ...
- ubuntu获取root权限
1.先在Terminal下设置root密码 执行 sudo passwd root 2.执行sudo su,然后根据提示来即可.
- vim -d file01 file02 diff file01 file02 对比两文件的不同
[root@86 vhosts]# vim -d defaul.conf.bak zabbix.xinxianm.com.conf server { | server { listen 80; | l ...
- 跟着百度学习之ThinkPHP的认识/初窥
MVC全称(Model View Controller) Model:模型(可以理解位数据库操作模型) View:视图(视图显示) Controller:(控制器) 简单的说框架就是一个类的集合.集合 ...
- PgSQl临时表的创建
创建前可先删除 drop table tmp0 创建临时表 select * into temp table tmp0 from xxx create index idx_tmp0_inner_cd ...
- 服务器操作系统应该选择 Debian/Ubuntu 还是 CentOS?
早期,我们使用 Debian 作为服务器软件,后来转向了CentOS,主要原因如下: 1.CentOS/RHEL的生命周期是7年,基本上可以覆盖硬件的生命周期,也就意味着一个新硬件安装以后,不用再次安 ...