为什么getResources().getColor(int)被弃用了?

3
文档说明:

此方法在API级别23中已被弃用。请改用getColor(int, Theme)

许多帖子指向ContextCompat.getColor(Context,int)方法作为替代品。

文档还解释了Theme参数:

主题Resources.Theme:用于样式化颜色属性的主题,可以为null。

您能解释一下主题如何影响颜色吗?
2个回答

4
一些复杂的颜色,比如android.content.res.GradientColor(在VectorDrawable中使用),需要一个主题才能填充梯度,因为你可能会有这样的定义:
<gradient xmlns:android="http://schemas.android.com/apk/res/android">
   <android:startColor="?android:attr/colorPrimary"/>
   <android:endColor="?android:attr/colorControlActivated"/>
   <android:type="linear"/>
 </gradient>

啊,我明白了。Resources类还有一个getColorStateList(int, Theme)方法,以及一个已弃用的版本。 - ssynhtn

0
您可以使用以下方法作为解决方案: ContextCompat.getColor(this, R.color.yourcolor)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接