分析Android多主题颜色的相关问题
作者:daisy 发布时间:2023-01-08 09:55:58
如果您通过以下的代码来获取定义的颜色值
context.getResources().getColor(R.color.some_color_resource_id);
在 Android Studio 中会有一个 lint 警告,提示您 Resources#getColor(int)
在 Marshmallow
中被废弃了,建议使用主题可知的 Resources#getColor(int, Theme
) 函数。 为了避免该警告,则可以使用 ContextCompat
:
ContextCompat.getColor(context, R.color.some_color_resource_id);
该函数的实现是这样的:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return context.getResources().getColor(id, context.getTheme());
} else {
return context.getResources().getColor(id);
}
看起来很简单。但是为什么会这样呢? 为什么会开始使用带主题的函数而废弃之前的函数呢?
Resources#getColor(int) & Resources#getColorStateList(int) 的问题
首先来看看这两个被废弃的函数是干啥的:
– Resources#getColor(int)
返回一个资源 id 对应的颜色值,如果该资源为 ColorStateList
则返回 ColorStateList
的默认颜色值
– Resources#getColorStateList(int)
返回对应的 ColorStateList
上面的代码在什么情况下会破坏我的代码呢?
要理解为何废弃这两个函数,来看个 ColorStateList 的例子。 当在 TextView 中使用自定义的 ColorStateList 的时候, TextView 不可用状态和可用状态的文字颜色分别使用 R.attr.colorAccent
和 R.attr.colorPrimary
表示。
XHTML
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorAccent" android:state_enabled="false"/>
<item android:color="?attr/colorPrimary"/>
</selector>
现在如果您通过如下的代码来获取这个ColorStateList
ColorStateList csl = context.getResources().getColorStateList(R.color.button_text_csl);
上面的代码会抛出一个异常(查看logcat 可以看到如下的信息)
W/Resources: ColorStateList color/button_text_csl has unresolved theme attributes!
Consider using Resources.getColorStateList(int, Theme)
or Context.getColorStateList(int)
at android.content.res.Resources.getColorStateList(Resources.java:1011)
...
哪里出错了呢?
问题的根源在于 Resources
对象并没有和一个 Theme
对象关联,当使用 R.attr.colorAccent
和 R.attr.colorPrimary
指代颜色的时候,在代码中通过上面的函数解析的时候没有指定对应的 Theme导致无法解析出结果。 所以在 Marshmallow
中添加了 ColorStateList
对 Theme 的支持并且添加了这两个新的函数:Resources#getColor(int, Theme)
和 Resources#getColorStateList(int, Theme),
并使用 Theme 参数来解析里面的 attributes
属性。
在新版本的 Support 库中也有对应的实现,分别位于 ResourcesCompat
和 ContextCompat
类中。
如何解决该问题呢?
使用 AppCompat v24+ 版本可以很容易的解决该问题。
ColorStateList csl = AppCompatResources.getColorStateList(context, R.color.button_text_csl);
在 23+ 版本上直接使用系统的函数,在之前的版本上 AppCompat 自己解析这些 xml 文件从里面提取 attr 属性指代的数值。 AppCompat 同时还支持 ColorStateList 新的 android:alpha
属性。
Resources#getDrawable(int) 的问题
Resources#getDrawable(int)
和前面的两个函数的问题是类似的。 在 Lollipop 之前的版本中无法支持 Theme attr 。
为啥我这样用也没有出现异常呢?
异常并不总是会出现。
VectorDrawableCompat
和 AnimatedVectorDrawableCompat
类中添加了和 AppCompatResources
类类似的功能。比如在 矢量图中你可以使用 ?attr/colorControlNormal
来设置矢量图的颜色,VectorDrawableCompat
会自动完成解析该 属性的工作:
XHTML
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
android:tint="?attr/colorControlNormal">
<path
android:pathData="..."
android:fillColor="@android:color/white"/>
</vector>
小测试
下面使用一个小测试来回顾一下前面介绍的内容。 假设有下面一个 ColorStateList:
XHTML
<!-- res/colors/button_text_csl.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorAccent" android:state_enabled="false"/>
<item android:color="?attr/colorPrimary"/>
</selector>
在应用中定义了如下的 Theme:
XHTML
<!-- res/values/themes.xml -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/vanillared500</item>
<item name="colorPrimaryDark">@color/vanillared700</item>
<item name="colorAccent">@color/googgreen500</item>
</style>
<style name="CustomButtonTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorPrimary">@color/brown500</item>
<item name="colorAccent">@color/yellow900</item>
</style>
在代码中有如下的函数用来解析颜色值并在代码中创建 ColorStateList:
@ColorInt
private static int getThemeAttrColor(Context context, @AttrRes int colorAttr) {
TypedArray array = context.obtainStyledAttributes(null, new int[]{colorAttr});
try {
return array.getColor(0, 0);
} finally {
array.recycle();
}
}
private static ColorStateList createColorStateList(Context context) {
return new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled}, // Disabled state.
StateSet.WILD_CARD, // Enabled state.
},
new int[]{
getThemeAttrColor(context, R.attr.colorAccent), // Disabled state.
getThemeAttrColor(context, R.attr.colorPrimary), // Enabled state.
});
}
看看是否能猜出在 API 19 和 API 23 版本上文字禁用状态和正常状态的颜色,实现代码如下(5和8的情况,在TextView xml 中指定了 android:theme=”@style/CustomButtonTheme”
):
Resources res = ctx.getResources();
// (1)
int deprecatedTextColor = res.getColor(R.color.button_text_csl);
button1.setTextColor(deprecatedTextColor);
// (2)
ColorStateList deprecatedTextCsl = res.getColorStateList(R.color.button_text_csl);
button2.setTextColor(deprecatedTextCsl);
// (3)
int textColorXml =
AppCompatResources.getColorStateList(ctx, R.color.button_text_csl).getDefaultColor();
button3.setTextColor(textColorXml);
// (4)
ColorStateList textCslXml = AppCompatResources.getColorStateList(ctx, R.color.button_text_csl);
button4.setTextColor(textCslXml);
// (5)
Context themedCtx = button5.getContext();
ColorStateList textCslXmlWithCustomTheme =
AppCompatResources.getColorStateList(themedCtx, R.color.button_text_csl);
button5.setTextColor(textCslXmlWithCustomTheme);
// (6)
int textColorJava = getThemeAttrColor(ctx, R.attr.colorPrimary);
button6.setTextColor(textColorJava);
// (7)
ColorStateList textCslJava = createColorStateList(ctx);
button7.setTextColor(textCslJava);
// (8)
Context themedCtx = button8.getContext();
ColorStateList textCslJavaWithCustomTheme = createColorStateList(themedCtx);
button8.setTextColor(textCslJavaWithCustomTheme);
下面是对应的实现截图:
总结


猜你喜欢
- 学习在于记录,把自己不懂得容易忘记得记录下,才是最好得选择。废话不多说,想要在Android开发中嵌入c/c++代码,直接开始如下步骤1、创
- 一、String类概述String类代表字符串,java程序中的所有字符串文字(例如"abc")都被实现为此类的实例。也
- 简介线段树是一种二叉搜索树,是用来维护区间信息的数据结构。可以在O(logN)的时间复杂度内实现单点修改、区间修改、区间查询(区间求和,求区
- 前言有时,我们可能需要从 PDF 文档中提取表格数据,例如,当PDF发票的表格中存储了一些有用的信息,需要提取数据以进行进一步分析时。在这篇
- 大家在使用 Intellij IDEA 的时候会经常遇到各种乱码问题,甚是烦扰。栈长也偶尔会用下IDEA,也有一些解决乱码的经验,我给大家总
- 在使用手机时,蓝牙通信给我们带来很多方便。那么在Android手机中怎样进行蓝牙开发呢?本文以实例的方式讲解Android蓝牙开发的知识。&
- 1.类的属性 filed1)在kotlin中定义属性,必须赋初始值,要不编译器检查不通过。这个和java不同2)kotlin会针对于定义的每
- 1 前言到目前为止Java仍然是使用最多的编程语言,随着Java以及Java社区的不断壮大,Java也早已不再是简简单单的一门计算机语言了,
- java提供了Comparable接口与Compatator接口,它们为数组或集合中的元素提供了排序逻辑,实现此接口的对象数组或集合可以通过
- 在日常生活中,我们使用maven下载需要的jar包,但是很多的时候由于中央仓库没有,所以我们没有办法下载到需要的jar包,手动去下载上,然后
- JVM java虚拟机JVMjava虚拟机是一个可执行java字节码的虚拟机进程。Java虚拟机本质上就是一个程序,java源文件被编译成能
- 1. 启用AOPa. 在类上添加@Aspect注解b. 注入该类, 可以使用@Component进行注入到Spring容器中2. 通过Poi
- 前言SpringBoot是Spring的包装,通过自动配置使得SpringBoot可以做到开箱即用,上手成本非常低,但是学习其实现原理的成本
- 核心考点:链表操作,临界条件检查,特殊情况处理在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针
- 前言嗯。最近工程上遇到一个byte数组转换为int的问题,解决过程中遇到了几个坑,经过各种查资料终于还是解决了。撒花。Java的位运算以及b
- 因为涉及到 io 流输入问题,如果不关闭会一直占用资源。所以使用过后要及时关闭,防止资源一直被占用。Scanner在使用前要导入 java.
- Java 分割字符串java.lang.String 的 split() 方法, JDK 1.4 or laterpublic
- Unity脚本中枚举类型在inspector面板中文显示,供大家参考,具体内容如下效果:工具脚本:ChineseEnumTool.csusi
- 一.简单介绍1.配置相关的依赖2.配置模式3写.mapper、controller、service4.配置yaml文件 配置mybatis全
- XmlTextReader 提供对 XML 数据流的只进只读访问。当前节点指读取器定位到的节点。使用任何读方法推进读取器并且属性反映当前节点