Laughing
getResources().getDrawable()过时问题解决
12/12
本文最后更新于2024年03月18日,已超过184天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
Android开发时,我们经常会动态指定按钮的背景图。
如果我们使用shareItem.setBackground(getResources().getDrawable(R.color.teal_200));
设置时,会收到Ide的提示,
我们有三种方式解决此问题:
1)使用drawable资源但不为其设置theme主题
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null); //null就是null,不设主题
``
如:myexample.setIcon(ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null));
2)使用默认的activity主题
ContextCompat.getDrawable(getActivity(), R.drawable.name); //getActivity(),如果是在activity里就直接用this
``
如:myexample.setIcon(ContextCompat.getDrawable(this,R.drawable.ic_favorite_black_18dp));
3)使用自定义主题
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
版权属于:
香草物语
本文链接:
https://www.xiangcaowuyu.net/web/getresources-getdrawable-out-of-date.html(转载时请注明本文出处及文章链接)
作品采用:
typecho挺好的