Java 将图像动态应用于菜单项
我正在尝试使用glide以编程方式向菜单项添加图像 主菜单.xmlJava 将图像动态应用于菜单项,java,android,xml,android-image,Java,Android,Xml,Android Image,我正在尝试使用glide以编程方式向菜单项添加图像 主菜单.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_pi
<?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" />
但是,我不能使用这种方法,因为资源是MenuItem而不是ImageView。将其强制转换为ImageView也不起作用
鉴于图像不在drawables文件夹中,由用户设置,因此也不能使用以下方法,如何实现预期结果:
itemProfilePic.setActionView(savedProfilePic)代码>接受整数值
最终结果
制作一个自定义工具栏,然后您可以使用图像视图动态更改图像
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
app:contentInsetStart="0dp"
android:background="@color/primaryColor"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/ic_perm_24dp"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
菜单项接口有一个方法:
获取要更新的菜单项的引用
将要设置的图像转换为图像李>
根据需要调整可拉伸的,并通过MenuItem.setIcon()
方法进行设置
看起来您还可以从正在使用的映像加载程序库中实现。自定义目标可以包装一个MenuItem
注意:
因为您正在膨胀XML菜单,所以很可能会刷新它。因此,在膨胀XML后也要执行图像图标更新。因此,基本上您要求菜单的整行是自定义的,还是仅仅是图像?我已经在end@lbra,是否尝试此@lbra Is菜单的可能副本,该菜单仅包含1个自定义图像项?在这种情况下,为什么不在右侧设置带有imageview的自定义工具栏呢?
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
app:contentInsetStart="0dp"
android:background="@color/primaryColor"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/ic_perm_24dp"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>