问题描述:引自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
}
});