本文翻译自:getResources().getColor() is deprecated [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

  • getColor(int id) deprecated on Android 6.0 Marshmallow (API 23) 19 answers 在Android 6.0 Marshmallow(API 23) 19答案中 弃用了getColor(int id)

Using: buildToolsVersion "22.0.1" , targetSdkVersion 22 in my gradle file. 使用: buildToolsVersion "22.0.1" ,我的gradle文件中的targetSdkVersion 22

I found that the useful getResources().getColor(R.color.color_name) is deprecated. 我发现不推荐使用有用的getResources().getColor(R.color.color_name)

What should I use instead? 我应该用什么呢?


#1楼

参考:https://stackoom.com/question/29boV/不推荐使用getResources-getColor-重复


#2楼

I found that the useful getResources().getColor(R.color.color_name) is deprecated. 我发现不推荐使用有用的getResources()。getColor(R.color.color_name)。

It is not deprecated in API Level 21, according to the documentation . 根据文档 ,它在API Level 21中不被弃用。

It is deprecated in the M Developer Preview. 在M开发者预览版弃用。 However, the replacement method (a two-parameter getColor() that takes the color resource ID and a Resources.Theme object) is only available in the M Developer Preview. 但是,替换方法(采用颜色资源ID和Resources.Theme对象的双参数getColor() )仅在M Developer Preview中可用。

Hence, right now, continue using the single-parameter getColor() method. 因此,现在,继续使用单参数getColor()方法。 Later this year, consider using the two-parameter getColor() method on Android M devices, falling back to the deprecated single-parameter getColor() method on older devices. 今年晚些时候,可以考虑使用这两个参数getColor()方法在Android M器件,回落至弃用单参数getColor()旧设备上的方法。


#3楼

It looks like the best approach is to use: 看起来最好的方法是使用:

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

eg: 例如:

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

This will choose the Marshmallow two parameter method or the pre-Marshmallow method appropriately. 这将适当地选择Marshmallow两参数方法或pre-Marshmallow方法。


#4楼

You need to use ContextCompat.getColor(), which is part of the Support V4 Library (so it will work for all the previous API). 您需要使用ContextCompat.getColor(),它是Support V4库的一部分(因此它适用于所有以前的API)。

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

As specified in the documentation, "Starting in M, the returned color will be styled for the specified Context's theme". 如文档中所述,“从M开始,返回的颜色将针对指定的Context主题设置样式”。 SO no need to worry about it. 所以不用担心。

You can add the Support V4 library by adding the following to the dependencies array inside your app build.gradle: 您可以通过将以下内容添加到app build.gradle中的dependencies数组来添加Support V4库:

compile 'com.android.support:support-v4:23.0.1'

#5楼

well it's deprecated in android M so you must make exception for android M and lower. 好吧,它在Android M中被弃用,所以你必须为android M和更低版本做例外。 Just add current theme on getColor function. 只需在getColor函数上添加当前主题即可。 You can get current theme with getTheme() . 您可以使用getTheme()获取当前主题。

This will do the trick in fragment, you can replace getActivity() with getBaseContext() , yourContext , etc which hold your current context 这将完成片段中的技巧,你可以用getBaseContext()yourContext等替换getActivity()来保存你当前的上下文

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {yourTitle.setTextColor(getActivity().getResources().getColor(android.R.color.white, getActivity().getTheme()));
}else {yourTitle.setTextColor(getActivity().getResources().getColor(android.R.color.white));
}

*ps : color is deprecated in M, but drawable is deprecated in L * ps:颜色在M中已弃用,但在L中不推荐使用drawable

不推荐使用getResources()。getColor()[重复]相关推荐

  1. getResources.getColor过时

    在android 23(6.0)及以上getResources.getColor(R.color.colorId)API过时时,那么它的替代方法为ContextCompat.getColor(cont ...

  2. java getcolor_如何使用新版本的getresources() . getColor?

    我试图使用 getresources.getColor(resource id) 获取一个颜色元素但是android告诉我它已被弃用 getresources.getColor(resource id ...

  3. android 解决getColor()方法过时

    今天,简单讲讲android里如何解决getColor()方法过时的问题. 之前,我写博客讲了程序员需要解决过时的方法的问题,Google会提供过时函数的替代函数,程序员有责任找到替代函数,并且解决过 ...

  4. 在Android 6.0 Marshmallow(API 23)上弃用了getColor(int id)

    本文翻译自:getColor(int id) deprecated on Android 6.0 Marshmallow (API 23) The Resources.getColor(int id) ...

  5. ListView优化机制及滑动时数据时出现的数据错乱重复问题 图片、checkBox等

    该篇内容主要是记录我在实际开发中遇到的ListView滑动时数据错乱的几种情况,以及解决方法.在进行ListView滑动时数据错乱问题讨论之前会对ListView所谓的<优化>进行说明.文 ...

  6. 推你所想,神策智能推荐 Demo 上线(可免费体验)

    根据麦肯锡咨询公司的数据显示,74% 的互联网用户对标准化内容心生反感,得当的个性化则会增加 10% 到 30% 的收入. 而如今众多企业的个性化推荐都是隔靴搔痒收效甚微,典型表现如下: 1.给用户推 ...

  7. 直播 | WWW 2021论文解读:生成式板块推荐的变分控制和评估

    「AI Drive」是由 PaperWeekly 和 biendata 共同发起的学术直播间,旨在帮助更多的青年学者宣传其最新科研成果.我们一直认为,单向地输出知识并不是一个最好的方式,而有效地反馈和 ...

  8. 【活动】师兄喊你来读博,优秀CV实验室推荐!

    点击我爱计算机视觉标星,更快获取CVML新技术 近期有不止一位52CV的群友向我咨询关于读博(或博后)选择学校的问题,尤其是对于香港和新加坡的学校,不少52CV的粉丝很感兴趣. 昨天恰好发布了一条:香 ...

  9. 算法高级(41)-推荐算法实现

    一.推荐系统 [维基百科]推荐系统是一种信息过滤系统,用于预测用户对物品的"评分"或"偏好". 推荐系统近年来非常流行,应用于各行各业.推荐的对象包括:电影.音 ...

最新文章

  1. layer 弹不出来_红米k20pro摄像头弹不出来怎么办?昆明小米手机维修教你解决
  2. Vue HTML:在Vue上写Html遇到的一些问题,html的首行缩进,html的行间距,element UI的回到顶部不显示
  3. 是否提交由npm 5创建的package-lock.json文件?
  4. 【学习・求职必备】可谓是相当Awesomes了,这些综述类的github项目你不可错过!...
  5. Notepad++加上xml格式化的功能
  6. 2017ACM/ICPC亚洲区沈阳站 C Hdu-6219 Empty Convex Polygons 计算几何 最大空凸包
  7. sun 些命令可以将服务器设置至ok模式_Python 高手之路:从零开始打造一个Web服务器...
  8. php mysql.so 下载_Linux安装php-mysql提示需要:libmysqlclient.so.18()(64bit)的解决办法-Go语言中文社区...
  9. 用户登录功能:说简单也困难
  10. 洛阳php编程培训,洛阳PHP开发培训分享提高PHP开发效率的四个编程习惯
  11. 题解【luogu P2421 bzoj P1407 [NOI2002]荒岛野人】
  12. php的json_encode第二个参数学习及应用
  13. paip.python 调用qt ui 总结
  14. 判断conime.exe是不是病毒!
  15. excel怎么打钩(excel怎么打钩符号)
  16. [转]如何进行人脉管理
  17. OSChina 周日乱弹 ——书中自有颜如玉
  18. springboot毕设项目基于的人才招聘系统6czpy(java+VUE+Mybatis+Maven+Mysql)
  19. 相对位置编码与绝对位置编码
  20. java中的值传递和引用传递2原文:http://blog.csdn.net/niuniu20008/article/details/2953785...

热门文章

  1. JDK 中的证书生成和管理工具 keytool
  2. MyBatis 拦截器(4)
  3. 由一道题目引发的为稳定与不稳定的排序思路
  4. PostgreSQL的backuplabel file 初步学习
  5. 使用Ext.grid.Panel生成表格
  6. Oracle内置存储过程之DBMS_OUTPUT
  7. CentOS第一次安装MySQL的完整步骤
  8. XP没有IIS服务组组件
  9. 统计一个字符串中英文字母、空格、数字和其它字符的个数
  10. axis 开发webservice