android option menu with icon

前端 未结 9 1087
借酒劲吻你
借酒劲吻你 2021-02-05 08:36

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.

Jav

相关标签:
9条回答
  • 2021-02-05 08:57

    the problem is the Androidmanifest.xml. Remove android:theme="@style/AppTheme" and it will work just fine

    0 讨论(0)
  • 2021-02-05 09:01

    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

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

    Now in 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);
          }
       }
    
    0 讨论(0)
  • 2021-02-05 09:05

    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;
    }
    
    0 讨论(0)
提交回复
热议问题