最近在使用这些方法的时候总是在网上搜,感觉很麻烦,所以在这里记忆一下。
getResources().getDrawable() 过时的解决方法:
1. 当你这个Drawable不受主题影响时
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
2.当你这个Drawable受当前Activity主题的影响时
ContextCompat.getDrawable(getActivity(), R.drawable.name);
3.当你这个Drawable想使用另外一个主题样式时
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
getColor()方法过时解决方案:
开发过程中遇到getColor()方法过时,Google给出的解决方案是:使用ContextCompat.getColor(context, R.color.color);
ContextCompat.getColor(context, R.color.colorPrimary);
//源码如下:
public static final int getColor(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 23) {
return ContextCompatApi23.getColor(context, id);
} else {
return context.getResources().getColor(id);
}
}