Android的getColor,getDrawable过时的替代方法

来源:互联网 发布:苹果手机淘宝比价插件 编辑:程序博客网 时间:2024/06/10 00:58

问题

Android SDK 升级到 6.0(API23) 之后,getDrawable和getColor方法提示过时。

解决方法

getResources().getColor 替换成 ContextCompat.getColor
getResources().getDrawable 替换成 ContextCompat.getDrawable

例子

int colorInt = getResources().getColor(R.color.colorAccent);//返回的是color的int类型值:-49023int colorInt2 = ContextCompat.getColor(this, R.color.colorAccent);//返回的是color的int类型值:-49023Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);Drawable drawable2 = ContextCompat.getDrawable(this,R.mipmap.ic_launcher);



原创粉丝点击