Resources.getColor(int id)
方法已被弃用。
@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException {
return getColor(id, null);
}
我该怎么办?
Resources.getColor(int id)
方法已被弃用。
@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException {
return getColor(id, null);
}
我该怎么办?
ResourcesCompat
的 getColor(Resources, int, Theme)
方法。int white = ResourcesCompat.getColor(getResources(), R.color.white, null);
ContextCompat
中的getColor(Context, int)
,这更好地反映了您关于Resources
的问题。在API级别23之前,主题不会被应用,方法调用会通过getColor(int)
进行,但您将不会收到已弃用的警告。主题也可能是null
。我也感到沮丧。我的需求非常简单。我只想从资源中获取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;
}
getColor(int)
: 这个方法在API 23级中已经被弃用。
请使用getColor(int, android.content.res.Resources.Theme)
代替。
我尝试了minSDK = 21
:
if(Build.VERSION.SDK_INT < 23) {
resources.getColor(R.color.rippelColor, null)
} else {
resources.getColor(R.color.rippelColor)
}