Java 将图像添加到菜单项
我的Java 将图像添加到菜单项,java,android,xml,Java,Android,Xml,我的res/drawable hdpi/delete.png中有.png图像,我想将其作为图标添加到我的菜单项中 我用过: delete.xml <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/deletebutton"> </bitmap> 看看这个: <?xml version="1.0" encoding="utf-8"?>
res/drawable hdpi/delete.png中有.png图像,我想将其作为图标添加到我的菜单项中
我用过:
delete.xml
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/deletebutton">
</bitmap>
看看这个:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/open" android:icon="@drawable/openicon"
android:title="Open the file" />
<item
android:id="@+id/save"
android:icon="@drawable/saveicon"
android:title="Save the file" />
</menu>
不需要设置位图文件,只需单独使用上面的行即可。希望这对你有帮助
按照本教程进行操作首先,在您的案例中确实不需要delete.xml
<item
android:id="@+id/delete"
android:showAsAction="always"
android:icon="@drawable/deletebutton"
android:title="@string
/>
在这里,您缺少右撇号和值。如果不想在菜单项中显示标题,只需删除此属性
更新:
您在loadDrawable()处有所以
,这意味着您的Drawable太大,无法加载(堆栈已完全填满)。尝试使用另一个(较小的)或修剪实际的。同时还要确保您的图像在所有可绘图文件夹中。android:background=“@drawable/ic\u play”/>是我正在做的,已经过期了,但我会给出一个答案供其他人将来参考,您可以像这样通过编程进行进一步的样式设置:
public class MyClass {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Do your other stuff here
MenuItem item = menu.getItem(0);
if (item.getItemId() == R.id.action_example) {
ImageView imageView = new ImageView(getActivity());
imageView.setMaxHeight(18);
imageView.setMaxWidth(18);
imageView.setImageResource(R.drawable.barchart32);
item.setActionView(imageView);
}
}
}
<item
android:id="@+id/delete"
android:showAsAction="always"
android:icon="@drawable/deletebutton"
android:title="@string
/>
android:title="@string
java.lang.StackOverflowError
public class MyClass {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Do your other stuff here
MenuItem item = menu.getItem(0);
if (item.getItemId() == R.id.action_example) {
ImageView imageView = new ImageView(getActivity());
imageView.setMaxHeight(18);
imageView.setMaxWidth(18);
imageView.setImageResource(R.drawable.barchart32);
item.setActionView(imageView);
}
}
}