This question already has answers here:
getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)

(13个回答)


5年前关闭。




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

我发现不推荐使用有用的getResources().getColor(R.color.color_name)

我应该怎么用呢?

最佳答案

看来最好的方法是使用:

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

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

这将选择棉花糖双参数法或预棉花糖方法适当。

关于android - getResources()。getColor()已过时[duplicate],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31842983/

10-11 22:23
查看更多