简体   繁体   中英

How to add icon to the menu of actionbar in android studio

My app has a menu (the default icon of menu is three dots) which contains several items. I want to add a icon to the menu not the icon to the actionbar. I research for 3 hours but all of the tutorials are about adding icons to the actionbar not the menu. Anybody knows how to do this or have the tutorial link?

在此处输入图片说明

On the top right

check this code ,

In your .java file add onCreateOptionsMenu code,

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_inbox, menu); //.xml file name
        return super.onCreateOptionsMenu(menu);
    }

Now, create this menu_inbox.xml as defined above in java code

<?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/action_websearch"
        android:icon="@mipmap/ic_launcher_menu_o"  //icon with three dots
        android:orderInCategory="3"  //number of item in it
        android:title="Info"         //title on longpress on menu icon
        app:showAsAction="ifRoom">   //to show menu on upper side

        <menu>
            <item
                android:id="@+id/action_reply"
                android:title="@string/reply" />

            <item
                android:id="@+id/action_replyall"
                android:title="@string/replyall" />

            <item
                android:id="@+id/action_forward"
                android:title="@string/forward" />
        </menu>
    </item>

</menu>

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