在 Android 6.0 Marshmallow (API 23) 上不推荐使用 getColor(int id) [英] getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)

查看:25
本文介绍了在 Android 6.0 Marshmallow (API 23) 上不推荐使用 getColor(int id)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我该怎么办?

推荐答案

从 Android Support Library 23 开始,
一个新的getColor() 方法已添加到 ContextCompat.

Starting from Android Support Library 23,
a new getColor() method has been added to ContextCompat.

来自官方 JavaDoc 的描述:

Its description from the official JavaDoc:

返回与特定资源 ID 关联的颜色

Returns a color associated with a particular resource ID

从 M 开始,返回的颜色将为指定的上下文主题设置样式.

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

所以,打电话:

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

您可以查看ContextCompat.getColor() GitHub 上的源代码.

这篇关于在 Android 6.0 Marshmallow (API 23) 上不推荐使用 getColor(int id)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆