本文翻译自:getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)
The Resources.getColor(int id)
method has been deprecated. 不推荐使用Resources.getColor(int id)
方法。
@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException {
return getColor(id, null);
}
What should I do? 我该怎么办?
#1楼
参考:https://stackoom.com/question/28YBe/在Android-Marshmallow-API-上弃用了getColor-int-id
#2楼
Starting from Android Support Library 23, 从Android支持库23开始,
a new getColor() method has been added to ContextCompat
. 在ContextCompat
添加了一个新的getColor()方法。
Its description from the official JavaDoc: 它来自官方JavaDoc的描述:
Returns a color associated with a particular resource ID 返回与特定资源ID关联的颜色
Starting in M, the returned color will be styled for the specified Context's theme. 从M开始,返回的颜色将针对指定的Context主题设置样式。
So, just call : 所以, 请致电 :
ContextCompat.getColor(context, R.color.your_color);
You can check the ContextCompat.getColor()
source code on GitHub . 您可以在GitHub上检查ContextCompat.getColor()
源代码 。
#3楼
tl;dr: TL;博士:
ContextCompat.getColor(context, R.color.my_color)
Explanation: 说明:
You will need to use ContextCompat.getColor() , which is part of the Support V4 Library (it will work for all the previous APIs). 您将需要使用ContextCompat.getColor() ,它是Support V4库的一部分(它适用于所有以前的API)。
ContextCompat.getColor(context, R.color.my_color)
If you don't already use the Support Library, you will need to add the following line to the dependencies
array inside your app build.gradle
(note: it's optional if you already use the appcompat (V7) library ): 如果您尚未使用支持库,则需要将以下行添加到app build.gradle
的dependencies
数组中(注意: 如果您已经使用appcompat(V7)库,则它是可选的 ):
compile 'com.android.support:support-v4:23.0.0' # or any version above
If you care about themes, the documentation specifies that: 如果您关心主题,则文档指定:
Starting in M, the returned color will be styled for the specified Context's theme 从M开始,返回的颜色将针对指定的Context主题设置样式
#4楼
In Android Marshmallow many methods are deprecated. 在Android Marshmallow中,许多方法都已弃用。
For example, to get color use 例如,获得颜色使用
ContextCompat.getColor(context, R.color.color_name);
Also to get drawable use 也是为了获得可绘制的用途
ContextCompat.getDrawable(context, R.drawable.drawble_name);
#5楼
I don't want to include the Support library just for getColor , so I'm using something like 我不想仅为getColor包含支持库,所以我使用的是类似的东西
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);
}
}
I guess the code should work just fine, and the deprecated getColor
cannot disappear from API < 23. 我想代码应该可以正常工作,并且不推荐使用的getColor
不能从API <23中消失。
#6楼
Even though getColor()
is deprecated in marshmallow, the replacement method 即使在marshmallow中弃用了getColor()
,也就是替换方法 getColor(int, Theme)
is available. getColor(int, Theme)
可用。 Which can be used as an alternative to ContextCompat.getColor(context, R.color.your_color);
哪个可以用作ContextCompat.getColor(context, R.color.your_color);
的替代品ContextCompat.getColor(context, R.color.your_color);