简体   繁体   中英

How to show Icon in drop down menu of PopupMenu

I want to add some icon in the drop-down menu of a popupMenu i created. please help me. I am so much bothered on how to create popupMenu to show dropdown menu items with icon when clicked. I have tried a lot of things which did not work. I tried to set showAsAction to always|withtext, but it didn't work. Please help me. Any help will be much appreciated!

Below is the Java

showMenuButton = findViewById(R.id.btn_long_press);


        //Init popup menu
        final PopupMenu popupMenu = new PopupMenu(
                this, //the context
                showMenuButton //UI view where to click to show the popup menu
        );

        //add menu xml to our popup menu
        popupMenu.getMenuInflater().inflate(R.menu.pop_menu, popupMenu.getMenu());

        //handle popup menu item clicks
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                //get id of menu item clicked
                int id = menuItem.getItemId();
                //handle clicks
                if (id==R.id.settings_menu){
                    //settings selected
                    Toast.makeText(MainActivity.this, "Settings Selected", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(MainActivity.this, Main2Activity.class));
                    getTitleColor();
                    return true;
                }
                else if (id==R.id.manual_menu){
                    //Manual selected
                    Toast.makeText(MainActivity.this, "User Manual Selected", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(MainActivity.this, Main3Activity.class));
                    return true;
                }
                else if (id==R.id.about_menu){
                    //about selected
                    Toast.makeText(MainActivity.this, "About Selected", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(MainActivity.this, Main4Activity.class));
                    return true;
                }

                return false;
            }
        });


        //handle button click to show menu
        showMenuButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupMenu.show();

            }
        });

this is the menu XML

<item
        android:id="@+id/settings_menu"
        android:title="Settings"
        android:icon="@drawable/ic_settings"
        app:showAsAction="always|withText" />

    <item
        android:id="@+id/manual_menu"
        android:title="User Manual"
        android:icon="@drawable/ic_developer" />

    <item
        android:id="@+id/about_menu"
        android:title="About"
        android:icon="@drawable/ic_about"/>

这就是我想要的样子

I discovered from my research that to show drop-down popup menu with menu item icon takes deep process. And the process is not clean. However, i have found a solution. Creating sub menu seems to be the only lee-way to get what we want. And I encourage anyone seeking to create menu icon to follow this way I founded for now while we wait further improvement from google.

<?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_more"
        android:icon="@android:drawable/ic_menu_more"
        android:orderInCategory="1"
        android:title="More Options »"
        app:showAsAction="always">


        <menu>

            <item
                android:id="@+id/settings_menu"
                android:title="Settings"
                android:icon="@drawable/ic_settings"/>

            <item
                android:id="@+id/manual_menu"
                android:title="User Manual"
                android:icon="@drawable/ic_developer" />

            <item
                android:id="@+id/about_menu"
                android:title="About"
                android:icon="@drawable/ic_about"/>

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