的getColor(INT ID)pcated在Android 6.0棉花糖德$ P $(API 23) [英] getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)

查看:423
本文介绍了的getColor(INT ID)pcated在Android 6.0棉花糖德$ P $(API 23)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Resources.getColor(INT ID)方法已经被德precated。

The Resources.getColor(int id) method has been deprecated.

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

我该怎么办?

What can I do?

推荐答案

更新22/08

这是Android的支持库23,一个新的<一个开始href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getColor(android.content.Context,%20int)">getColor()方法已被添加到ContextCompat

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

所以,只要致电:

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

从官方的JavaDoc说明:

Description from the official JavaDoc:

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

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() <一个href="https://github.com/android/platform_frameworks_support/blob/master/v4/java/android/support/v4/content/ContextCompat.java">source从支持库code :

public static final int getColor(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompatApi23.getColor(context, id);
    } else {
        return context.getResources().getColor(id);
    }
}

这篇关于的getColor(INT ID)pcated在Android 6.0棉花糖德$ P $(API 23)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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