带图标的弹出式菜单

时间:2016-05-15 15:40:12

标签: android android-layout android-menu

我是android的新手。我无法在弹出菜单中添加简单的图标。 我将图标添加到xml文件中但我看不到它们。

这是我的菜单 - pop_up_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_email"
        android:icon="@mipmap/email"
        android:title="E-Mail"
        />
    <item
        android:id="@+id/action_messenger"
        android:icon="@mipmap/messenger"
        android:title="Messenger"
        />
    <item
        android:id="@+id/action_skype"
        android:icon="@mipmap/skype"
        android:title="Skype"
        />
    <item
        android:id="@+id/action_whatsapp"
        android:icon="@mipmap/whatsapp"
        android:title="Whatsapp"
        />
</menu>

我有这个OnClickListener在点击moreActionsButton后调用showPopUpMenu函数

moreActionsButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showPopUpMenu(us);
    }
});

这是创建菜单的showPopUpMenu

public void showPopUpMenu(final User user) {
    View menuItemView = getView().findViewById(R.id.groupLeave);
    PopupMenu popUpMenu = new PopupMenu(getActivity(), menuItemView);
    popUpMenu.getMenuInflater().inflate(R.menu.pop_up_menu, popUpMenu.getMenu());
    popUpMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_messenger:
                    onMessengerClick(user);
                    break;

                case R.id.action_skype:
                    onSkypeClick(user);
                    break;

                case R.id.action_whatsapp:
                    onWhatsappClick(user);
                    break;

                case R.id.action_email:
                    onEmailClick(user);
                    break;

                default:
                    break;

            }
            return true;
        }
    });
    popUpMenu.show();
}

1 个答案:

答案 0 :(得分:5)

默认情况下,PopupMenu不会在标题旁边显示图标。

您需要做的是创建自己的PopupMenu实现并通过覆盖默认构造函数强制使用图标:

 public CustomPopupMenu(Context context, View anchor) {
       ...
       mPopup.setForceShowIcon(true);
    }

在显示PopupMenu之前,使用反射来调用所需的方法

        //... initialization of your PopupMenu
        Object menuHelper;
        Class[] argTypes;
        try {
            Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
            fMenuHelper.setAccessible(true);
            menuHelper = fMenuHelper.get(popupMenu);
            argTypes = new Class[]{boolean.class};
            menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
        } catch (Exception e) {
            // Possible exceptions are NoSuchMethodError and NoSuchFieldError
            //
            // In either case, an exception indicates something is wrong with the reflection code, or the
            // structure of the PopupMenu class or its dependencies has changed.
            //
            // These exceptions should never happen since we're shipping the AppCompat library in our own apk,
            // but in the case that they do, we simply can't force icons to display, so log the error and
            // show the menu normally.
        }
        popUpMenu.show();