android - 在 Android 6.0 Marshmallow (API 23) 上不推荐使用 getColor(int id)

标签 android android-resources android-6.0-marshmallow android-mnc

Resources.getColor(int id) 方法已被弃用。

@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException {
    return getColor(id, null);
}

我该怎么办?

最佳答案

从 Android 支持库 23 开始,
一个新的getColor()方法已添加到 ContextCompat

来自官方JavaDoc的描述:

Returns a color associated with a particular resource ID

Starting in M, the returned color will be styled for the specified Context's theme.


所以,只需调用:

ContextCompat.getColor(context, R.color.your_color);

您可以检查 ContextCompat.getColor() source code on GitHub .

关于android - 在 Android 6.0 Marshmallow (API 23) 上不推荐使用 getColor(int id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31590714/

相关文章:

java - 如何正确地将图像检索到 Android 项目的 res/drawable/文件夹中?

android - 从 UsageStatsManager Android 获取以前日期的应用程序使用数据

android - 在Android中录制音频时设置封面图像

android - 根据屏幕密度/大小了解(以编程方式)使用哪个资源文件夹的最简单方法

android - 在 gradle 中声明 android.support.v7.app

android - RelativeLayout 支持多种屏幕尺寸

android - 在通话应用中替换

android - 需要为 Android 6.0 上的蓝牙低功耗扫描启用位置

java - 在列表中显示数据时出现问题

android - Android : what to do with low devicePixelRatio devices: tablets, 智能手机等网站?