今天,简单讲讲android里如何解决getColor()方法过时的问题。

之前,我写博客讲了程序员需要解决过时的方法的问题,Google会提供过时函数的替代函数,程序员有责任找到替代函数,并且解决过时的函数。所以,我检测代码时发现getColor()方法已经过时,自己在网上查找了资料,找到了替代函数,解决了问题。

getColor()过时过时的源码:

 /*** Returns a color integer associated with a particular resource ID. If the* resource holds a complex {@link ColorStateList}, then the default color* from the set is returned.** @param id The desired resource identifier, as generated by the aapt*           tool. This integer encodes the package, type, and resource*           entry. The value 0 is an invalid identifier.** @throws NotFoundException Throws NotFoundException if the given ID does*         not exist.** @return A single color value in the form 0xAARRGGBB.* @deprecated Use {@link #getColor(int, Theme)} instead.*/@ColorInt@Deprecatedpublic int getColor(@ColorRes int id) throws NotFoundException {return getColor(id, null);}

新替代getColor()的源码:

 /*** Returns a color associated with a particular resource ID* <p>* Starting in {@link android.os.Build.VERSION_CODES#M}, the returned* color will be styled for the specified Context's theme.** @param id The desired resource identifier, as generated by the aapt*           tool. This integer encodes the package, type, and resource*           entry. The value 0 is an invalid identifier.* @return A single color value in the form 0xAARRGGBB.* @throws android.content.res.Resources.NotFoundException if the given ID*         does not exist.*/@ColorIntpublic static final int getColor(Context context, @ColorRes int id) {final int version = Build.VERSION.SDK_INT;if (version >= 23) {return ContextCompatApi23.getColor(context, id);} else {return context.getResources().getColor(id);}}

在新的方法中进行了判断,进行6.0系统的区分,针对于6.0以下还是调用了原来的getColor方法,对于6.0+的使用了新的方法进行替代,这个就不用说了吧,一般的升级都会对老版本进行兼容,具体的使用方法也稍有变化

过时getColor()方法使用:

过时getColor()方法使用:

新的getColor()方法使用:

这里还提一下getDrawable过时的替代方法,基本是一样的。

用getDrawable()方法过时了

现象

网友推荐

谷歌查询结果

android 解决getColor()方法过时就讲完了。

就这么简单。



android 解决getColor()方法过时相关推荐

  1. 解决FragmentPagerAdapter方法过时

    用到ViewPager和TabLayout时,发现FragmentPagerAdapter()方法过时 class MyAdapter(fm: FragmentManager?, fragmentLi ...

  2. Android开发managedQuery方法过时如何解决

    今天在获取手机相册照片的时候用到了managedQuery,然后发现managedQuery过时了,如下图所示: 于是就百度了一下解决办法,其实也挺简单的.用getContentResolver(). ...

  3. HttpClient4.5实现http与https请求,解决之前方法过时问题

    前言 最近项目正在筹备上线,发现 HttpClient 中的很多类和方法都已经过时了,虽然并不影响功能的实现,但是看着确实让人捉急,所以,索性抽了点时间把它给改了.今天就来分享一下... 之前的代码 ...

  4. android 解决setbackgrounddrawable过时

    今天,简单讲讲android如何解决  setbackgrounddrawable过时的问题. 之前,自己也讲了一些函数的过时的替代函数,昨天,自己在代码里又发现了setbackgrounddra ...

  5. Redis 配置连接池,redisTemplate 操作多个db数据库,切换多个db,解决JedisConnectionFactory的设置连接方法过时问题。(转)

    Redis 配置连接池,redisTemplate 操作多个db数据库,切换多个db,解决JedisConnectionFactory的设置连接方法过时问题.(转) 参考文章: (1)Redis 配置 ...

  6. 我的Android进阶之旅------gt;Android字符串资源中的单引號问题error: Apostrophe not preceded by 的解决的方法...

    刚刚在string字符串资源文件里,写了一个单引號.报错了,错误代码例如以下 error: Apostrophe not preceded by \ (in OuyangPeng's blog ) 资 ...

  7. android 解决password过时

    今天,简单讲讲如何解决android:password 过时的问题.  这个其实很简单,之前自己讲过很多过时的代码,这个android:password 过时也经常遇到,只是一直没有花时间写成博客 ...

  8. android 解决getNetworkInfo过时

    今天,简单讲讲android里如何解决getNetworkInfo过时的问题. 之前,我写了一篇博客,讲如何获取手机是否连网,其中有的代码用到了ConnectivityManager 的getNetw ...

  9. Android Bitmap OutOfMemory 解决的方法

    在Android应用里,最耗费内存的就是图片资源.并且在Android系统中.读取位图Bitmap时,分给虚拟机中的图片的堆栈大小仅仅有8M.假设超出了.就会出现OutOfMemory异常 E/And ...

最新文章

  1. Open Drug Discovery Toolkit
  2. 人体关键点颜色和预处理
  3. TensorFlow tf.nn.conv2d是怎样实现卷积的?
  4. Oracle11gExp导出空表方法
  5. 为 Visual Studio 安装数据库工具
  6. 使用Presto SQL一些常见问题总结
  7. 华为交换机恢复出厂设置的三种方法
  8. DongTai被动型IAST工具部署
  9. 安信天行全方位信息安全态势感知平台建设与运营
  10. 计算机表格中的乘法怎么用,excel表格中怎么使用乘法公式
  11. 12306html布局,12306-Assistant
  12. iphone铃声android铃声,iphone12如何设置铃声?iphone12更换铃声方式分享[多图]
  13. 由于找不到C:\InetPub\ftproot\Tipray\Ldterm\ghijt32.DLL,无法继续执行代码。重新安装程序可能会解决此问题。
  14. Completed 404 NOT_FOUND,Whitelabel Error Page
  15. VSCODE 关闭文件和资源管理器关联
  16. C. Chocolate Bunny(思维+规律)
  17. Http--跨域请求
  18. 如何从 Github 中删除提交
  19. 使用swagger org.fasterxml.classmate ResolvedParameterizedMember找不到
  20. 个人每年结汇金额为5万美金,如果超过了限额,怎么结汇?义乌个体户500万大额结汇帮你

热门文章

  1. C# JArray与JObject 的使用
  2. 关于OnPaint、FormPaint会不停的触发 触发多次的情形---讨论总结
  3. NHibernate中的API
  4. 物理磁盘空间使用已满导致数据库hang起
  5. 转载的孩子们注意节操哈!!!
  6. 数据库切换时的一个傻逼的错误。
  7. Git常用命令使用大全
  8. 第八天- linux目录结构详细介绍
  9. GoF的23个经典设计模式
  10. apache的斜杠问题