Popup menu with icon on Android

前端 未结 8 1774
耶瑟儿~
耶瑟儿~ 2020-12-16 21:02

My menu xml code menu.xml:

         


        
8条回答
  •  隐瞒了意图╮
    2020-12-16 21:06

    You can enable icons for popup menu by using Java reflection to call a hidden method as below:

    public static void setForceShowIcon(PopupMenu popupMenu) {
        try {
            Field[] fields = popupMenu.getClass().getDeclaredFields();
            for (Field field : fields) {
                if ("mPopup".equals(field.getName())) {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popupMenu);
                    Class classPopupHelper = Class.forName(menuPopupHelper
                            .getClass().getName());
                    Method setForceIcons = classPopupHelper.getMethod(
                            "setForceShowIcon", boolean.class);
                    setForceIcons.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题