getResources().getColor() 已弃用 [英] getResources().getColor() is deprecated

查看:44
本文介绍了getResources().getColor() 已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用:buildToolsVersion "22.0.1" ,targetSdkVersion 22 在我的 gradle 文件中.

Using: buildToolsVersion "22.0.1" , targetSdkVersion 22 in my gradle file.

我发现有用的 getResources().getColor(R.color.color_name) 已被弃用.

I found that the useful getResources().getColor(R.color.color_name) is deprecated.

我应该用什么代替?

推荐答案

看起来最好的方法是使用:

It looks like the best approach is to use:

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

例如:

yourView.setBackgroundColor(ContextCompat.getColor(applicationContext,
                            R.color.colorAccent))

这将适当地选择棉花糖二参数方法或棉花糖前方法.

This will choose the Marshmallow two parameter method or the pre-Marshmallow method appropriately.

这篇关于getResources().getColor() 已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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