770

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

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

What should I do?

4

13 回答 13

1437

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

它来自官方 JavaDoc 的描述:

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

从 M 开始,返回的颜色将为指定的 Context 的主题设置样式。


所以,只需调用

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

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

于 2015-07-23T14:52:01.057 回答
516

tl;博士:

ContextCompat.getColor(context, R.color.my_color)

解释:

您将需要使用ContextCompat.getColor(),它是 Support V4 库的一部分(它适用于所有以前的 API)。

ContextCompat.getColor(context, R.color.my_color)

如果您还没有使用支持库,则需要将以下行添加到dependencies应用程序内的数组中build.gradle(注意:如果您已经使用了 appcompat (V7) 库,则它是可选的):

compile 'com.android.support:support-v4:23.0.0' # or any version above

如果您关心主题,文档会指定:

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

于 2015-08-21T21:08:36.573 回答
52

我不想只为getColor包含 Support 库,所以我使用类似的东西

public static int getColorWrapper(Context context, int id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.getColor(id);
    } else {
        //noinspection deprecation
        return context.getResources().getColor(id);
    }
}

我想代码应该可以正常工作,并且不推荐使用的getColor不能从 API < 23 中消失。

这就是我在 Kotlin 中使用的:

/**
 * Returns a color associated with a particular resource ID.
 *
 * Wrapper around the deprecated [Resources.getColor][android.content.res.Resources.getColor].
 */
@Suppress("DEPRECATION")
@ColorInt
fun getColorHelper(context: Context, @ColorRes id: Int) =
    if (Build.VERSION.SDK_INT >= 23) context.getColor(id) else context.resources.getColor(id);
于 2015-09-11T12:29:51.620 回答
31

在 Android Marshmallow 中,许多方法已被弃用。

例如,获取颜色使用

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

也可以获得可绘制的使用

ContextCompat.getDrawable(context, R.drawable.drawble_name);
于 2015-08-28T05:07:02.437 回答
31

对于所有的 Kotlin 用户:

context?.let {
    val color = ContextCompat.getColor(it, R.color.colorPrimary)
    // ...
}
于 2018-05-21T13:28:24.027 回答
9

在 Kotlin 中,您可以执行以下操作:

ContextCompat.getColor(requireContext(), R.color.stage_hls_fallback_snackbar)

如果 requireContext() 可以从您调用该函数的位置访问。尝试时出现错误

ContextCompat.getColor(context, R.color.stage_hls_fallback_snackbar)
于 2020-06-24T18:04:34.217 回答
6

在活动中使用 ContextCompat

ContextCompat.getColor(context, R.color.color_name)

在阿达珀

private Context context;


context.getResources().getColor()
于 2021-02-19T13:39:43.670 回答
4

在 Kotlin 的 RecyclerView 中

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun bind(t: YourObject, listener: OnItemClickListener.YourObjectListener) = with(itemView) {
        textViewcolor.setTextColor(ContextCompat.getColor(itemView.context, R.color.colorPrimary))
        textViewcolor.text = t.name
    }
}
于 2019-07-10T17:51:35.217 回答
2

如果您当前的最小值。API级别是23,你可以getColor()像我们用来获取字符串资源一样简单地使用getString()

//example
textView.setTextColor(getColor(R.color.green));
// if `Context` is not available, use with context.getColor()

您可以限制低于 23 的 API 级别:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    textView.setTextColor(getColor(R.color.green));
} else {
    textView.setTextColor(getResources().getColor(R.color.green));
}

但为简单起见,您可以像下面这样接受答案:

textView.setTextColor(ContextCompat.getColor(context, R.color.green))

资源

来自ContextCompat AndroidX

来自ContextCompat 支持

于 2020-01-12T05:16:30.620 回答
1

使用Android 支持库中的getColor(Resources, int, Theme)方法。ResourcesCompat

int white = ResourcesCompat.getColor(getResources(), R.color.white, null);

我认为它比您询问的问题更能反映您的getColor(Context, int)问题。在 API 级别 23 之前,将不会应用主题并且方法会调用到,但您不会收到已弃用的警告。主题也可能是。ContextCompatResourcesgetColor(int)null

于 2015-11-18T21:08:38.740 回答
1

如果您不一定需要这些资源,请使用parseColor(String)
Color.parseColor("#cc0066")

于 2018-04-27T03:22:48.590 回答
0

我也很沮丧。我的需求非常简单。我想要的只是资源中的 ARGB 颜色,所以我写了一个简单的静态方法。

protected static int getARGBColor(Context c, int resId)
        throws Resources.NotFoundException {

    TypedValue color = new TypedValue();
    try {
        c.getResources().getValue(resId, color, true);
    }
    catch (Resources.NotFoundException e) {
        throw(new Resources.NotFoundException(
                  String.format("Failed to find color for resourse id 0x%08x",
                                resId)));
    }
    if (color.type != TYPE_INT_COLOR_ARGB8) {
        throw(new Resources.NotFoundException(
                  String.format(
                      "Resourse id 0x%08x is of type 0x%02d. Expected TYPE_INT_COLOR_ARGB8",
                      resId, color.type))
        );
    }
    return color.data;
}
于 2016-10-01T19:18:29.347 回答
0

最好的等价物是使用ContextCompat.getColorand ResourcesCompat.getColor。我为快速迁移做了一些扩展功能:

@ColorInt
fun Context.getColorCompat(@ColorRes colorRes: Int) = ContextCompat.getColor(this, colorRes)

@ColorInt
fun Fragment.getColorCompat(@ColorRes colorRes: Int) = activity!!.getColorCompat(colorRes)

@ColorInt
fun Resources.getColorCompat(@ColorRes colorRes: Int) = ResourcesCompat.getColor(this, colorRes, null)
于 2021-02-08T08:08:38.113 回答