简体   繁体   English

将图像添加到菜单项

[英]add image to menu item

I have .png images in my res/drawable-hdpi/delete.png I want to add it as icon to my menu item . 我在res/drawable-hdpi/delete.png有.png图像我想将它作为图标添加到我的菜单item

I used: 我用了:

delete.xml delete.xml

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

Menu activity_main.xml 菜单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>

But its not working. 但它不起作用。

I removed the xml file and tried directly to load the image here is the log file. 我删除了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

At first you really don't need delete.xml in your case. 首先,在你的情况下你真的不需要delete.xml

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

And also i think you have problem here: 而且我认为你在这里有问题:

android:title="@string

Here, you are missing right apostrophe and value. 在这里,你缺少右撇号和价值。 If you don't want to show title in MenuItem just remove this property. 如果您不想在MenuItem中显示标题,只需删除此属性即可。

Update: 更新:

java.lang.StackOverflowError

You have SO at loadDrawable() and it means that your Drawable is too big to load(you stack is filled up completely). 您在loadDrawable()中有SO ,这意味着您的Drawable太大而无法加载(您的堆栈已完全填满)。 Try to use another(smaller) or trim actual one. 尝试使用另一个(较小的)或修剪实际的一个。 And also make sure you have your image in all drawable folders. 并确保您在所有可绘制文件夹中都有您的图像。

It's overdue but I'll Give an answer just for future reference by others, you can do further styling programatically like so: 它已经过期,但我会给出一个答案,仅供其他人参考,你可以像以下那样以编程方式做进一步的样式:

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

Have a look at this: 看看这个:

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

There is no need to set bitmap files, just use above lines alone. 无需设置位图文件,仅使用上面的行。 Hope this will help you. 希望这会帮助你。

Follow this tutorial Menus in Android 在Android中按照本教程菜单操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM