在android中添加上下文菜单图标

Den*_*nie 16 icons android android-context

我有一个带有ContextMenu的Listview,但是当我为ContextMenu设置setIcon时,它看起来不起作用

public void onCreateContextMenu(ContextMenu menu , View v, 
        ContextMenuInfo menuInfo){

    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.context_menu_favorite)
        .setIcon(android.R.drawable.btn_star);      
}
Run Code Online (Sandbox Code Playgroud)

Com*_*are 39

上下文菜单不支持图标.

注意:上下文菜单项不支持图标或快捷键.

  • @androiddeveloper:这个答案下周就满十岁了。从那时起,文档发生了变化。 (4认同)

Hir*_*tel 8

我是这样做的:

参考截图:

在此处输入图片说明

菜单:

menu_patient_language.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activities.PatientSelectionActivity">

    <item
        android:id="@+id/menuEnglish"
        android:icon="@drawable/language_english"
        android:title="@string/english" />

    <item
        android:id="@+id/menuFrench"
        android:icon="@drawable/language_french"
        android:title="@string/french" />

</menu>
Run Code Online (Sandbox Code Playgroud)

风格:

样式.xml:

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->

        <item name="android:popupMenuStyle">@style/popupMenuStyle</item>

    </style>

  <!--- Language selection popup -->

    <style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:itemBackground">@android:color/white</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

爪哇代码:

 private void showPopup(View v) {

        Context wrapper = new ContextThemeWrapper(this, R.style.popupMenuStyle);
        PopupMenu mypopupmenu = new PopupMenu(wrapper, v);

        setForceShowIcon(mypopupmenu);
        MenuInflater inflater = mypopupmenu.getMenuInflater();
        inflater.inflate(R.menu.menu_patient_language, mypopupmenu.getMenu());
        mypopupmenu.show();
//        mypopupmenu.getMenu().getItem(0).setIcon(getResources().getDrawable(R.mipmap.ic_launcher));
        mypopupmenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                txtPreferredLanguage.setText(item.getTitle().toString());
                switch (item.getItemId()) {
                    case R.id.menuEnglish:
                        // Your code goes here
                        break;

                    case R.id.menuFrench:
                        // Your code goes here
                        break;
                }
                return false;
            }
        });
    }

    private void setForceShowIcon(PopupMenu popupMenu) {
        try {
            Field[] mFields = popupMenu.getClass().getDeclaredFields();
            for (Field field : mFields) {
                if ("mPopup".equals(field.getName())) {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popupMenu);
                    Class<?> popupHelper = Class.forName(menuPopupHelper.getClass().getName());
                    Method mMethods = popupHelper.getMethod("setForceShowIcon", boolean.class);
                    mMethods.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

希望这会帮助你确定。

完毕