textView.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0)方法无法获取设置系统应用图标的问题

<span style="font-size:12px;">PackageInfo packageInfo = packageInfos.get(position);
                int icon = packageInfo.applicationInfo.icon;
                if (icon!= 0) {
                        System.out.println(icon);
                        textView.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
                }else {
                        textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
                }</span>
原本想在获取应用对应的图标id之后,通过
<span style="font-size:12px;">textView.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);</span>

设置textView的图片,可是设置之后总是报错,找不到该资源。后来通过查看public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)方法的源码。找到了原因:

<span style="font-size:12px;">public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) {
       <span style="color:#FF0000;"> final Resources resources = getContext().getResources();</span>
        setCompoundDrawablesWithIntrinsicBounds(left != 0 ? resources.getDrawable(left) : null,
                top != 0 ? resources.getDrawable(top) : null,
                right != 0 ? resources.getDrawable(right) : null,
                bottom != 0 ? resources.getDrawable(bottom) : null);
    }</span>

从 final Resources resources = getContext().getResources();可知,获取的Resource是本应用的,而非系统的,再根据id肯定找不着应用对应的图标了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值