Is it possible to display icons in a PopupMenu?PopupMenu中显示图片是否可能

问题描述:引自http://stackoverflow.com/questions/6805756/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:
我真的很喜欢这个来自于3.0以后就能使用的控件popupmenu,但是我就是无法在它的子项中展示图片,我将.xml中填充的菜单贴在下面

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

With this code:
使用这些代码

image.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        PopupMenu pop = new PopupMenu(getActivity(), v);
        pop.getMenuInflater().inflate(R.menu.shelves_details_menu, pop.getMenu());
        (这里写上面的try/catch语句)
        pop.show();
    }
});

I can’t get the icons to show up, am I missing something?

Thanks.

解决办法:同一stackoverflow下某答案
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());
                Field mPopup = classPopupMenu.getDeclaredField("mPopup");
                mPopup.setAccessible(true);
                Object menuPopupHelper = 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();
            }

我自己使用的例子
e.g.

        deleteBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                PopupMenu popup = new PopupMenu(getApplication(), deleteBtn);
                popup.getMenuInflater().inflate(
                        R.menu.clickitem_menu_userselect, popup.getMenu());

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                        case 0:
                            deleteBtn.setText(R.string.report);
                            // deleteBtn.setBackgroundResource(R.drawable.reporttiezi);
                            deleteBtn.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                }
                            });
                            // sdf
                            reportTidorPid(mContext, null);
                            return true;
                        case 1:
                            if (CheckNetwork.conncetState) {
                                // 获取评论
                                Intent intent = new Intent();
                                intent.setClass(mContext,
                                        MoreReplyActivity.class);
                                intent.putExtra("tid", tid);
                                intent.putExtra("mainpid", mainPid);
                                startActivity(intent);
                            } else {
                                InfoNetworkToast();
                            }

                            return true;
                        case 2:

                            if (CheckNetwork.conncetState) {
                                deleteTiezi();// 是否删除帖子
                            } else {
                                InfoNetworkToast();
                            }

                            return true;
                        default:
                            return false;
                        }
                    }
                });
                try {
                    Class<?> classPopupMenu = Class.forName(popup.getClass()
                            .getName());
                    Field mPopup = classPopupMenu.getDeclaredField("mPopup");
                    mPopup.setAccessible(true);
                    Object menuPopupHelper = mPopup.get(popup);
                    Class<?> classPopupHelper = Class.forName(menuPopupHelper
                            .getClass().getName());
                    Method setForceIcons = classPopupHelper.getMethod(
                            "setForceShowIcon", boolean.class);
                    setForceIcons.invoke(menuPopupHelper, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                popup.show(); // showing popup menu
            }
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值