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

来源:互联网 发布:大庆三打一网络直播 编辑:程序博客网 时间:2024/06/06 01:06

本文用于解决getDrawable()被划掉、波浪线等问题:

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); 
阅读全文
0 0