Android的 - ResourcesNotFoundException试图通过资源ID设置操作栏图标时,图标、操作、资源、Android

2023-09-12 02:55:15 作者:[遇]

我改变我的操作栏图标 CircleImageView 显示资料图片:

I am changing my Action Bar icon to CircleImageView that shows profile picture:

CircleImageView actionBarIcon_Profile = new CircleImageView(this);
actionBarIcon_Profile.setImageDrawable(getResources().getDrawable(R.drawable.my_picture));
actionBarIcon_Profile.setLayoutParams(new LayoutParams(48, 48));
actionBarIcon_Profile.setId(R.id.actionBarIcon_Profile);

getActionBar().setIcon(R.id.actionBarIcon_Profile);

ID被保存在ids.xml:

The ids are kept in ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="actionBarIcon_Profile"/>
</resources>

我得到ResourcesNotFoundException。任何帮助将AP preciated。

I get ResourcesNotFoundException. Any help will be appreciated.

推荐答案

的setIcon 能接收一个资源的ID。该ID actionBarIcon_Profile 就是你要创建一个没有与之相关联的资源的一个唯一的ID。这就是为什么你得到 ResourcesNotFoundException

setIcon is expecting to receive an id of a resource. That id actionBarIcon_Profile is just an unique id you're creating that has no resource associated with it. That's why you're getting ResourcesNotFoundException.

您可以设置绘制你的动作条的的setIcon 方法。不是一个ImageView的。您需要创建一个圆形的ImageView的圆形绘制代替。

You can set a drawable to your actionbar's setIcon method. Not an imageview. You need to create a circular drawable instead of a circular imageView.