zoukankan      html  css  js  c++  java
  • Is it possible to display icons in a PopupMenu?

    I really like the new PopupMenu we got in 3.0, but I just can't display any icons next to the menu items in it. I'm inflating the menu from the .xml below:

    <item android:id="@+id/menu_delete_product"
        android:icon="@drawable/sym_action_add"
        android:title="delete"
        android:showAsAction="ifRoom|withText" />
    
    <item android:id="@+id/menu_modify_product"
        android:icon="@drawable/sym_action_add"
        android:title="modify"
        android:showAsAction="ifRoom|withText" />
    
    <item android:id="@+id/menu_product_details"
        android:icon="@drawable/sym_action_add"
        android:title="details"
        android:showAsAction="ifRoom|withText" />

    If you're willing to be a bit adventurous, look at Google's source code for PopupMenu. Create your own class i.e. MyPopupMenu that is the same as Google's PopupMenu class, but make one slight change.

    In PopupMenu's constructor:

    public MyPopupMenu(Context context, View anchor) {
        // TODO Theme?
        mContext = context;
        mMenu = new MenuBuilder(context);
        mMenu.setCallback(this);
        mAnchor = anchor;
        mPopup = new MenuPopupHelper(context, mMenu, anchor);
        mPopup.setCallback(this);
        mPopup.setForceShowIcon(true); //ADD THIS LINE
    
    }

    use the method setForceShowIcon to force it to show the icon. You can also just expose a public method to set this flag as well depending on your needs.

    Contribution to the solution provided by Gaelan Bolger. Use this code if you get a "IllegalAccessException: access to field not allowed".

    PopupMenu popup = new PopupMenu(mContext, view);
    
    try {
        Field[] fields = popup.getClass().getDeclaredFields();
        for (Field field : fields) {
            if ("mPopup".equals(field.getName())) {
                field.setAccessible(true);
                Object menuPopupHelper = field.get(popup);
                Class<?> classPopupHelper = Class.forName(menuPopupHelper
                        .getClass().getName());
                Method setForceIcons = classPopupHelper.getMethod(
                        "setForceShowIcon", boolean.class);
                setForceIcons.invoke(menuPopupHelper, true);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    prepareMenu(popup.getMenu());
    popup.show();

    I was able to show the icons using reflection. It may not be the most elegant solution but it works.

    try {
                    Class<?> classPopupMenu = Class.forName(popupMenu
                            .getClass().getName());
                    Object menuPopupHelper = classPopupMenu.getDeclaredField(
                            "mPopup").get(popupMenu);
                    Class<?> classPopupHelper = Class.forName(menuPopupHelper
                            .getClass().getName());
                    Method setForceIcons = classPopupHelper.getMethod(
                            "setForceShowIcon", boolean.class);
                    setForceIcons.invoke(menuPopupHelper, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

    http://stackoverflow.com/questions/6805756/is-it-possible-to-display-icons-in-a-popupmenu

  • 相关阅读:
    配置Hibernate的二级缓存
    shiro安全三部曲
    将 Shiro 作为应用的权限基础 五:SpringMVC+Apache Shiro+JPA(hibernate)整合配置
    将 Shiro 作为应用的权限基础 四:shiro的配置说明
    将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程
    将 Shiro 作为应用的权限基础 二:基于SpringMVC实现的认证过程
    将 Shiro 作为应用的权限基础 一:shiro的整体架构
    基于Spring框架的Shiro配置
    shior笔记
    每天学习点jquery
  • 原文地址:https://www.cnblogs.com/savagemorgan/p/4103075.html
Copyright © 2011-2022 走看看