Android6.0+解决getColor()方法过时
来源:互联网 发布:对焦算法 编辑:程序博客网 时间:2024/06/05 18:18
最近发现看到别人编译代码的时候都是用的6.0往上的版本,我还在用5.0的,瞬间有点伤感啊,伤感自己不能与时俱进,玻璃心啊,不多说了,今天做一下笔记,关于getResources().getColor()方法过时的替代方法,在Android的6.0以上的编译环境中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 @Deprecated public 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. */ @ColorInt public 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()方法使用:
可能是个人代码习惯,就是不愿意看到代码中有那些过时,警告。。。。。
所以个人还是比较偏向于代码洁癖习惯的形成
阅读全文
1 0
- Android6.0+解决getColor()方法过时
- AndroidStudio getColor方法过时
- getColor过时方法替代
- 解决Android中getResources().getColor()方法过时的问题
- Android getColor方法提示过时
- getResources().getColor()方法过时的替代方法
- getDrawable,getColor 过时的替代方法
- getColor方法在6.0中已经过时
- Android 6.0 的getColor()方法过时
- getDrawable,getColor 过时的替代方法
- Android getResources().getColor() 过时 替代方法
- 过时的getResources().getColor()的替代方法
- getColor过时,setTextColor的现用方法
- getColor()方法过时的解决办法
- Android getResources().getColor() 过时 替代方法
- Android-6.0-23的getColor()过时替代方法
- Android开发中过时方法总结getColor,singeLine
- [问题解决]Android中getDrawable和getColor过时的替代方法
- 关于 Java 数组的 12 个最佳方法
- 线程同步–生产者与消费者模式
- numpy数组广播详解
- 阿里云的Linux中,express创建的服务器遇到的坑
- docker在web开发中得使用流程是怎样的?
- Android6.0+解决getColor()方法过时
- java 求1+2!+3!+...+20!的和
- CodeVs1039[数的划分]
- 创建podsepc
- Volley的简单使用
- poj 3233 Matrix Power Series(矩阵里面的元素也是矩阵)
- 小程序之初识心得
- NLP知识笔记
- 在html页面嵌入applet异常处理办法