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);
}
我该怎么办?
从 Android Support Library 23 开始,
一个新的 getColor() 方法已经添加到 ContextCompat
中。
官方 JavaDoc 中对其进行了描述:
返回与特定资源 ID 相关联的颜色
从 M 开始,返回的颜色将基于指定上下文的主题进行样式化。
因此,只需调用:
ContextCompat.getColor(context, R.color.your_color);
你可以在GitHub上检查ContextCompat.getColor()
的源代码。简而言之:
ContextCompat.getColor(context, R.color.my_color)
说明:
您需要使用ContextCompat.getColor(),它是Support V4 Library的一部分(它适用于所有先前的API)。
ContextCompat.getColor(context, R.color.my_color)
build.gradle
文件中的dependencies
数组中添加以下行(注意:如果你已经使用了appcompat(V7)库则是可选的):compile 'com.android.support:support-v4:23.0.0' # or any version above
如果您关注主题,文档指定:
从 M 开始,返回的颜色将针对指定上下文的主题进行样式设置
M
开始,返回的颜色将针对指定上下文主题进行样式设置。” - Bugs HappenContextCompat
类来自 SupportV4。AppcompatV7 也可以使用,因为它依赖于 SupportV4。正如 Support Library documentation 中所说,This library depends on the v4 Support Library. If you are using Ant or Eclipse, make sure you include the v4 Support Library as part of this library's classpath.
。因此,在答案中不将 AppcompatV7
放入其中是有道理的。 - Melvin int colorTwitterBlue = ContextCompat.getColor(this, R.color.color_twitter_blue);
composeTweetAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(colorTwitterBlue);
composeTweetAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(colorTwitterBlue);
- Lara Ruffle Coles我不想仅仅为了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);
对于所有的 Kotlin 用户:
context?.let {
val color = ContextCompat.getColor(it, R.color.colorPrimary)
// ...
}
val color = ContextCompat.getColor(context, R.color.colorPrimary)
。变量 "it" 可以是任何东西,但它需要是一个 __Context__。 - SMBiggsit
是 context
,因为我使用 context?.let {
来检查 context
是否为非空。函数 getColor()
只接受非空的 context
。在此处阅读有关 let
及其用法的更多信息:https://kotlinlang.org/docs/reference/scope-functions.html#let - Paul Spiesberger在Android Marshmallow中,许多方法已经被弃用。
例如,要获取颜色,请使用
ContextCompat.getColor(context, R.color.color_name);
也可以使用以下代码获取可绘制对象:
ContextCompat.getDrawable(context, R.drawable.drawble_name);
ContextCompat.getColor(requireContext(), R.color.stage_hls_fallback_snackbar)
如果您在调用函数的位置可以访问requireContext(),那么请继续使用它。我在尝试时遇到了一个错误。
ContextCompat.getColor(context, R.color.stage_hls_fallback_snackbar)
在活动中使用了ContextCompat。
ContextCompat.getColor(context, R.color.color_name)
在适配器中
private Context context;
context.getResources().getColor()
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
}
}
ContextCompat.getColor
和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)
如果您当前的最小API级别为23,则可以像使用getString()
获取字符串资源一样,简单地使用getColor()
:
//example
textView.setTextColor(getColor(R.color.green));
// if `Context` is not available, use with context.getColor()
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 Support)的ContextCompat。