Loading... > `getResource.getDrawable()`以及`getResource.getColos()`这一类方法已然呗Google废弃,我们现在需要使用新的方法去实现 #### ContextCompat ```java ContextCompat.getColor(); ContextCompat.getDrawable(); ``` 使用`ContextCompat.getDrawable();`时相当于 ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return resources.getDrawable(id, context.getTheme()); } else { return resources.getDrawable(id); } ``` 参数为`context` 和`resId` 这个方法获取到的资源会受到主题属性的控制 #### ResourcesCompat ```java ResourcesCompat.getDrawable(getResources(), R.drawable.name, null); ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme); ``` 参数的最后一个可以人为控制当前的资源是否拥有主题属性 最后修改:2022 年 03 月 17 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏