admin管理员组文章数量:1571373
今天,简单讲讲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
@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()方法使用:
新的getColor()方法使用:
这里还提一下getDrawable过时的替代方法,基本是一样的。
用getDrawable()方法过时了
现象
网友推荐
谷歌查询结果
android 解决getColor()方法过时就讲完了。
就这么简单。
版权声明:本文标题:android 解决getColor()方法过时 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1727690353a1125791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论