ContextCompat和ResourcesCompat的区别

  • 611 字

getResource.getDrawable()以及getResource.getColos()这一类方法已然呗Google废弃,我们现在需要使用新的方法去实现

ContextCompat

1
2
ContextCompat.getColor();
ContextCompat.getDrawable();

使用ContextCompat.getDrawable();时相当于

1
2
3
4
5
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(id, context.getTheme());
} else {
return resources.getDrawable(id);
}

参数为contextresId 这个方法获取到的资源会受到主题属性的控制

ResourcesCompat

1
2
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);

参数的最后一个可以人为控制当前的资源是否拥有主题属性

赞助喵
非常感谢您的喜欢!
赞助喵
分享这一刻
让朋友们也来瞅瞅!