简体   繁体   中英

Add icon to menu item from java code (android 2.3)

Is there a possibility, to add an image to menu item, from java code (not from xml file)?

For example (in XML):

item android:id="@+id/help" 
     android:icon="@drawable/ic_help"
     android:title="@string/help"

Second line adds a picture (icon) to an item, but this is XML file.

In code, when I'm making a menu there is only few constructor to add an item For example: public boolean onCreateOptionsMenu(Menu menu){

    menu.add(0,0,0,"Czerwony");

But anyone with image (only title text/string).

menu.add((0,0,0,"Czerwony").setIcon(R.drawable.edit_icon);

另请参阅本文: 如何在Java代码中的Android中创建菜单

You can use menu.add(0, 0, 0, "Czerwony").setIcon(int iconRes);

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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