博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Is it possible to display icons in a PopupMenu?
阅读量:4562 次
发布时间:2019-06-08

本文共 2642 字,大约阅读时间需要 8 分钟。

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:

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

posted on
2014-11-17 10:45 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/savagemorgan/p/4103075.html

你可能感兴趣的文章
RecyclerView的2种监听方式
查看>>
java语言基础第三讲作业
查看>>
iOS-Swift中的递增(++)和递减(--)被取消的原因
查看>>
Walk 解题报告
查看>>
爬虫综合大作业
查看>>
哈弗曼编码
查看>>
android 开机自启动
查看>>
这个SpringMVC的一直刷屏的问题你见过吗?无解
查看>>
自定义状态栏中的UIActivityIndicatorView
查看>>
我的2015年度总结
查看>>
2017-5-16/网站性能测试指标及网站压力测试
查看>>
Open CV 播放视频(2)
查看>>
object-c基础学习 基于<iOS软件开发揭秘>
查看>>
Scoreboard and Tomasulo
查看>>
sentinel控制台
查看>>
selenium 难定位元素,时间插件,下拉框定位,string包含,定位列表中的一个,技巧...
查看>>
【转】一些数据格式化-Eval( " ")和DataBinder.Eval(Container.DataItem, " ")的区别及用法...
查看>>
斗地主算法的设计与实现(四)--对牌进行排序
查看>>
How to get web browser history using cursor
查看>>
软键盘覆盖EditText解决方法
查看>>