getDrawable(int id) getColor(int id) is deprecate 已过时
来源:互联网 发布:知其心然后能救其失也 编辑:程序博客网 时间:2024/06/05 01:46
写代码是发现:
推荐使用:
为了兼容高、低版本 可以采用
ContextCompat.getColor(Context context, int id);
以下为getColor(int id)源码(Resource.java):
/** * 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(@ColorRes int id, @Nullable Theme theme)
/** * Returns a themed 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. * @param theme The theme used to style the color attributes, may be * {@code null}. * * @throws NotFoundException Throws NotFoundException if the given ID does * not exist. * * @return A single color value in the form 0xAARRGGBB. */@ColorIntpublic int getColor(@ColorRes int id, @Nullable Theme theme) throws NotFoundException { final TypedValue value = obtainTempTypedValue(); try { final ResourcesImpl impl = mResourcesImpl; impl.getValue(id, value, true); if (value.type >= TypedValue.TYPE_FIRST_INT && value.type <= TypedValue.TYPE_LAST_INT) { return value.data; } else if (value.type != TypedValue.TYPE_STRING) { throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid"); } final ColorStateList csl = impl.loadColorStateList(this, value, id, theme); return csl.getDefaultColor(); } finally { releaseTempTypedValue(value); }}
为了兼容高、低版本 可以采用
ContextCompat.getColor(Context context, int id);
代码如下:
/** * 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); }}
ContextCompatApi23.getColor(context, id) 方法代码如下:
class ContextCompatApi23 { public static ColorStateList getColorStateList(Context context, int id) { return context.getColorStateList(id); } public static int getColor(Context context, int id) { return context.getColor(id); }}
context.getColor(id) 方法代码如下:
/** * Returns a color associated with a particular resource ID and styled for * the current 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 final int getColor(@ColorRes int id) { return getResources().getColor(id, getTheme());}
getDrawable(int id) 采用了类似于 getColor(int id) 方法
阅读全文
0 0
- getDrawable(int id) getColor(int id) is deprecate 已过时
- getColor(int id) is deprecated
- getColor(int id)在API23时过时
- getColor(int id)在API23过时问题
- 'getColor(int)' is deprecated ,getColor过时
- 【Android】getColor(int id)在API23时过时
- 'getDrawable(int)' is deprecated,getDrawable过时
- getResources().getDrawable(int id) 替代
- getDrawable(int) is deprecated
- Resources的getDrawable(int id)方法源码详解
- Android getResource().getColor(int)过时解决方法
- getDrawable,getColor 过时的替代方法
- getDrawable()和getColor()过时的解决办法
- 过时的getResources().getColor,和getResources().getDrawable
- getDrawable,getColor 过时的替代方法
- Resource()中的getDrawable(int id)是怎样将图片显示出来的
- Resource()中的getDrawable(int id)是怎样将图片显示出来的
- Android Studio问题解决系列—Resources中getDrawable(int)过时
- windows下“gitk代码中文注释乱码”
- 汇编语言——寄存器(第二章)
- 写给开发者看的关系型数据库设计
- 最小生成树 ,prim 和Kruskal 算法
- ab网站压力测试工具
- getDrawable(int id) getColor(int id) is deprecate 已过时
- android 更改TextView部分字体颜色、大小、背景
- Nginx配置文件优化实例与参数详解
- android shape
- 路由器的DHCP功能及配置命令
- Cocoa App icon 命名&大小
- Eclipse分支及其介绍
- Nginx项目实战优化
- Android 设置横屏或竖屏