如何在PopupMenu的下拉菜单中显示图标

How to show Icon in drop down menu of PopupMenu

我想在我创建的弹出菜单的下拉菜单中添加一些图标。请帮我。我对如何创建 popupMenu 以在单击时显示带有图标的下拉菜单项感到非常困扰。我尝试了很多没有用的东西。我尝试将 showAsAction 设置为 always|withtext,但没有用。 请帮我。任何帮助将不胜感激!

下面是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();

            }
        });

这是菜单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"/>

我从研究中发现,显示带有菜单项图标的 drop-down 弹出菜单需要很深的过程。而且过程不干净。 但是,我找到了解决方案。创建子菜单似乎是唯一 lee-way 得到我们想要的东西。在我们等待 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>