将图像添加到菜单项

时间:2013-04-02 11:59:28

标签: java android xml

我的res/drawable-hdpi/delete.png中有.png图片我想将其作为图标添加到我的菜单item

我用过:

delete.xml

<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/deletebutton">
</bitmap>

菜单 activity_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/create"
    android:showAsAction="always"
    android:title="@string/createTask"/>
<item
    android:id="@+id/delete"
    android:showAsAction="always"
    android:icon="@drawable/delete"
    android:title="@string/deleteTask" />
</menu>

但它不起作用。

我删除了xml文件并尝试直接加载图像,这里是日志文件。

04-02 17:46:50.887: E/AndroidRuntime(11957): FATAL EXCEPTION: main
04-02 17:46:50.887: E/AndroidRuntime(11957): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.devicecontrolpanel/com.example.devicecontrolpanel.Main}: android.content.res.Resources$NotFoundException: Resource ID #0x7f020008
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.os.Looper.loop(Looper.java:156)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.main(ActivityThread.java:4987)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at java.lang.reflect.Method.invoke(Method.java:511)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at dalvik.system.NativeStart.main(Native Method)
04-02 17:46:50.887: E/AndroidRuntime(11957): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020008
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.content.res.Resources.getValue(Resources.java:1105)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.content.res.Resources.getDrawable(Resources.java:674)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.widget.CompoundButton.setButtonDrawable(CompoundButton.java:189)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at com.example.devicecontrolpanel.Main.listAllAlarms(Main.java:69)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at com.example.devicecontrolpanel.Main.onCreate(Main.java:25)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.Activity.performCreate(Activity.java:4538)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
04-02 17:46:50.887: E/AndroidRuntime(11957):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
04-02 17:46:50.887: E/AndroidRuntime(11957):    ... 11 more

4 个答案:

答案 0 :(得分:12)

首先,你真的不需要delete.xml

<item
    android:id="@+id/delete"
    android:showAsAction="always"
    android:icon="@drawable/deletebutton"
    android:title="@string
/>

而且我认为你在这里有问题:

android:title="@string

在这里,你缺少右撇号和价值。如果您不想在 MenuItem 中显示标题,只需删除此属性。

更新

java.lang.StackOverflowError

你在loadDrawable()中有 SO ,这意味着 Drawable 太大而无法加载(你的堆栈已完全填满)。尝试使用另一个(较小的)或修剪实际的一个。并确保在所有可绘制文件夹中都有图像。

答案 1 :(得分:4)

它已经过期,但我会给出一个答案,仅供其他人参考,你可以像程序化那样做进一步的样式:

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);
        }
    }
}

答案 2 :(得分:0)

看看这个:

<?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>

无需设置位图文件,只需单独使用上面的行。希望这会帮助你。

按照本教程Menus in Android

答案 3 :(得分:0)

android:background="@drawable/ic_play" />就是我在做什么,