简体   繁体   English

带图标的android选项菜单

[英]android option menu with icon

How to show icon with option menu.I have tried the following code but my option menu is without image icon.I am using android version 4.0 for developing app.如何使用选项菜单显示图标。我尝试了以下代码,但我的选项菜单没有图像图标。我正在使用 android 4.0 版开发应用程序。

Java code : Java代码

 public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
             menu.add("Add Contacts").setIcon(
                    R.drawable.ic_launcher);

            return true;
        }

Following is my app's screen shot以下是我的应用程序的屏幕截图

在此处输入图片说明

I need image to be displayed on the top of "Add Contacts" item.我需要图像显示在“添加联系人”项目的顶部。

Override OnPrepareOptionsMenu and add icon from there too覆盖OnPrepareOptionsMenu并从那里添加图标

and if its for above 3.0, use android:showAsAction in xml.如果它高于 3.0, android:showAsAction在 xml 中使用android:showAsAction

eg.例如。 android:showAsAction="ifRoom|withText"

I tried the code in two line and it works:我在两行中尝试了代码并且它有效:

public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add("Add Contacts");
        menu.getItem(0).setIcon(R.drawable.ic_launcher);
        return true;
}

You can create a custom menu like this:您可以像这样创建自定义菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/add_contacts"
          android:icon="@drawable/ic_launcher"
          android:title="@string/add_contacts"
         />
</menu>

And then inflate it然后充气

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}

More on this here: http://developer.android.com/guide/topics/ui/menus.html#options-menu更多关于这里: http : //developer.android.com/guide/topics/ui/menus.html#options-menu

you can directly set this into the xml file.您可以直接将其设置到xml文件中。

  <item android:id="@+id/add_contacts"
  android:icon="@android:drawable/plus_icon"
  android:title="Add Contacts"/>

You Can try Following this Link .您可以尝试关注此链接

Check this out and tell me if it worked or not.检查这个并告诉我它是否有效。

Or you can do some thing like this.或者你可以做一些这样的事情。
Create menu.xml创建 menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:id="@+id/next"
              android:icon="@drawable/ic_next"
              android:title="@string/next" />
      <item android:id="@+id/previous"
            android:icon="@drawable/ic_previous"
            android:title="@string/previous" />
      <item android:id="@+id/list"
            android:icon="@drawable/ic_list"
            android:title="@string/list" /> 
</menu>

And now you will be able to set ICON on menu现在您可以在菜单上设置 ICON

Now in CreateOptionMenu现在在 CreateOptionMenu

public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      return true;
    }

And to access that menu.并访问该菜单。

public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.next:
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.next) + " menu option",
                        Toast.LENGTH_SHORT).show();
            return true;
      …
      default:
            return super.onOptionsItemSelected(item);
      }
   }

To enable Option Menu with Icons:启用带图标的选项菜单:

Wrap the Items with an Item with showAsAction="always" and a Menu :包裹物品与ItemshowAsAction="always"和一个Menu

<?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:title="@string/title_menu"
        android:icon="@mipmap/ic_icon_menu"
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/action1"
                android:orderInCategory="100"
                android:title="@string/action1"
                android:icon="@mipmap/ic_icon_action1"
                app:showAsAction="never" />
        </menu>
    </item>
</menu>

If you use some following attribute in manifest file then it's will be show your icon....如果您在清单文件中使用以下属性,那么它将显示您的图标....

<activity android:name=".ui.CategoryActivity"
        android:label="@string/app_name"
        **android:theme="@android:style/Theme.NoTitleBar"**></activity>

It's work fine for me...:) +1 for my own effort...这对我来说很好用......:) +1 为我自己的努力......

**must be enter. **必须输入。

Easiest way is to use the @drawable only when setting your menu item.最简单的方法是仅在设置菜单项时使用 @drawable。

OR要么

Simply put the @drawable before the title declaration.只需将@drawable 放在标题声明之前。

<?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/addToFavorites"
  android:icon = "@drawable/ic_favorite_border_white_24dp"
  android:title = "Hello"
  app:showAsAction="always" />
<item
  android:id      ="@+id/about"
  android:title   ="About"
  app:showAsAction="never" />
</menu>  

the problem is the Androidmanifest.xml.问题是 Androidmanifest.xml。 Remove android:theme="@style/AppTheme" and it will work just fine删除android:theme="@style/AppTheme"就可以了

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

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