【发布时间】:2019-01-14 05:03:46
【问题描述】:
我正在尝试使用 glide 以编程方式将图像添加到菜单项。
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_profile_pic"
android:icon="@mipmap/profile_pic"
android:orderInCategory="100"
android:title=""
app:showAsAction="always" />
Java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.mipmap.profile_pic);
requestOptions.error(R.mipmap.profile_pic);
MenuItem itemProfilePic = menu.getItem(0);
//View view = findViewById(R.id.action_profile_pic);
if (savedProfilePic != null) {
Glide.with(this)
.setDefaultRequestOptions(requestOptions)
.load(Uri.parse(savedProfilePic))
.into(itemProfilePic);
}
return true;
但是,我不能使用这种方法,因为资源是 MenuItem 而不是 ImageView。将其投射到 ImageView 也不起作用。
如果图像不在drawables文件夹中,我怎样才能达到预期的效果,它是由用户设置的,因此下面的方法也不能使用:
itemProfilePic.setActionView(savedProfilePic); 接受一个整数值
最终结果
【问题讨论】:
-
所以基本上你需要整行菜单是自定义的还是只是图像?
-
我已经编辑了帖子,最后是自定义图片
-
@lbra,试试这个stackoverflow.com/questions/19882443/…
-
@lbra 菜单是否仅包含 1 个带有自定义图像的项目?在那种情况下,为什么您没有右侧带有 imageview 的自定义工具栏?
标签: java android xml android-image