首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PopupMenu图标没有显示

PopupMenu图标没有显示
EN

Stack Overflow用户
提问于 2017-05-26 07:18:30
回答 3查看 5.3K关注 0票数 4

我创建了一个ListView,它在每个项目中都有一个PopupMenu。我创建了一个menu layout并使用它作为我的PopupMenu。我的问题是,每次单击ListView项中的省略号选项时,PopupMenu都会与Text一起显示,但Icon不会出现。

这是我的xml菜单:

代码语言:javascript
复制
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_retry"
    android:icon="@drawable/retry"
    android:title="@string/retry"
    app:showAsAction="always"
    />
<item
    android:id="@+id/action_delete"
    android:icon="@drawable/delete"
    android:title="@string/delete"
    app:showAsAction="always"
    />
 </menu>

那么在我的Adapter中,我的ListView是这样的:

代码语言:javascript
复制
public class MyListViewAdapter extends BaseAdapter implements MenuItem.OnMenuItemClickListener {

.....

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = activity.getLayoutInflater();
        convertView = inflater.inflate(R.layout.mylistrow, null);
    }

    final View action = (View) convertView.findViewById(R.id.action);

    action.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            showPopup(action, position);
                      }
    });

    // ....codes for listview creation....

    return convertView;
}

public void showPopup(View v, int listItemPosition) {
    PopupMenu popup = new PopupMenu(mContext, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.outbox_menu, popup.getMenu());
    popup.show();
}

@Override
public boolean onMenuItemClick(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_retry:

            return true;
        case R.id.action_delete:

            return true;
        default:
            return false;
    }
}

提前谢谢你的帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-26 08:26:14

我在这个链接中找到了一个解决方案,并将它应用到我的代码中。

代码语言:javascript
复制
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();
    }
票数 12
EN

Stack Overflow用户

发布于 2017-05-26 07:28:29

MenuBuilder是一个隐藏类,但包含一个显示图标的方法。您需要使用反射来显示菜单中的图标。尝试在showPopop(View, int)中添加以下内容

代码语言:javascript
复制
PopupMenu popup = new PopupMenu(mContext, v);
try {
  Method method = popup.getMenu().getClass().getDeclaredMethod("setOptionalIconsVisible", boolean.class);
  method.setAccessible(true);
  method.invoke(popup.getMenu(), true);
} catch (Exception e) {
  e.printStackTrace();
}
票数 12
EN

Stack Overflow用户

发布于 2022-08-02 12:09:33

用这个代替android.widget.PopupMenu

代码语言:javascript
复制
androidx.appcompat.widget.PopupMenu popupMenu = new PopupMenu(this,view, Gravity.END);
popupMenu.setForceShowIcon(true);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44195825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档