TypechoJoeTheme

香草物语

统计
登录
用户名
密码
/
注册
用户名
邮箱
输入密码
确认密码

getResources().getDrawable()过时问题解决

Laughing博主
2020-12-12
/
0 评论
/
1,929 阅读
/
88 个字
/
百度已收录
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); 
Android Studio
朗读
赞(1)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

香草物语

本文链接:

https://www.xiangcaowuyu.net/web/getresources-getdrawable-out-of-date.html(转载时请注明本文出处及文章链接)

评论 (0)
  1. 小白 闲逛
    MacOS · Google Chrome

    typecho挺好的

    2020-09-26 回复