Android中getDrawable和getColor过时的替代方法

版权声明:本文为博主原创文章,未经博主允许不得转载。

前言

Android SDK 升级到 23 之后,getDrawable和getColor方法提示过时。

解决方案

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

例子如下:
int colorInt = getResources().getColor(R.color.colorAccent);//返回的是color的int类型值:-49023
int colorInt2 = ContextCompat.getColor(this, R.color.colorAccent);//返回的是color的int类型值:-49023

Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
Drawable drawable2 = ContextCompat.getDrawable(this,R.mipmap.ic_launcher);

参考资料

getDrawable,getColor 过时的替代方法

http://blog.csdn.net/u011368551/article/details/50886884 

原文地址:https://www.cnblogs.com/whycxb/p/6851583.html