简体   繁体   English

Android 工具栏弹出菜单不显示图标

[英]Android Toolbar Popup menu not showing icons

I am trying to show a drop down menu for my toolbar which includes BOTH text and icons:我正在尝试为我的工具栏显示一个下拉菜单,其中包括文本和图标:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_add"
        android:title="@string/menu.add"
        android:icon="@drawable/ic_add_black_24dp"
        app:showAsAction="always" />

    <item
        android:id="@+id/menu_edit"
        android:title="@string/menu.edit"
        android:icon="@drawable/ic_create_black_24dp"
        app:showAsAction="never" />

</menu>

The menu_add does show with the icon on the toolbar itself but the menu_edit only shows the text without the icon. menu_add 确实在工具栏本身上显示了图标,但 menu_edit 只显示了没有图标的文本。

This answer: https://stackoverflow.com/a/19750717/197127 says that Google has removed it by design but does not refer to how you may override it.这个答案: https://stackoverflow.com/a/19750717/197127说谷歌已经设计删除它但没有提到你可以如何覆盖它。

Edit编辑

I also need the device "menu" button to show the same menu.我还需要设备“菜单”按钮来显示相同的菜单。

Yes, its not being displayed in new versions of support library, but you can do the trick by adding submenu.是的,它不会显示在新版本的支持库中,但您可以通过添加子菜单来解决这个问题。 (You can add it via both xml and code). (您可以通过 xml 和代码添加它)。 like this:像这样:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
        android:title="@string/action_settings"
        app:showAsAction="always">
        <menu>
            <item
                android:icon="@drawable/ic_event"
                android:title="@string/action_settings"
                app:showAsAction="always" />
            <item
                android:icon="@drawable/ic_event"
                android:title="@string/action_settings"
                app:showAsAction="always" />
        </menu>
    </item>
</menu>

Hope it helped:) Edit: see snapshots of above code:希望它有所帮助:) 编辑:查看上述代码的快照:

菜单 => =>菜单打开

I created something like this:我创建了这样的东西:

@SuppressLint("RestrictedApi")
fun Menu.showIcons() {
    (this as? MenuBuilder)?.setOptionalIconsVisible(true)
}

and it works basically on any menu.它基本上适用于任何菜单。

For toolbar, you can override onPrepareOptionsMenu in activity or fragment and before calling super just call menu.showIcons() or you can use it with PopupMenu like so PopupMenu(requireContext(), anchor).menu.showIcons() .对于工具栏,您可以在活动或片段中覆盖onPrepareOptionsMenu并且在调用super之前只需调用menu.showIcons()或者您可以将它与PopupMenu一起使用,例如PopupMenu(requireContext(), anchor).menu.showIcons()

This Kotlin code worked for me.这个 Kotlin 代码对我有用。 I used "popup.setForceShowIcon(true)"我使用了“popup.setForceShowIcon(true)”

val popup = PopupMenu(context, button); val popup = PopupMenu(上下文,按钮);

popup.menuInflater.inflate(menuRes, popup.menu); popup.menuInflater.inflate(menuRes, popup.menu);

popup.setForceShowIcon(true) popup.setForceShowIcon(真)

The full code is written below完整代码写在下面

//R.menu.menu_popup -- Menu xml File
<menu
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_1"
        android:icon="@drawable/email"
        android:title="Email" />
    <item android:id="@+id/menu_2"
        android:icon="@drawable/phone"
        android:title="Phone" />
</menu>

// Inside Activity
findViewById<Button>(R.id.button1).setOnClickListener {
    show_PopUp_Menu(this, it, R.menu.menu_popup)
}


@SuppressLint("RestrictedApi")
fun show_PopUp_Menu(context: Context, button:Button, menuRes: Int) {

    val popup = PopupMenu(context, button)
    popup.menuInflater.inflate(menuRes, popup.menu)

    popup.setOnMenuItemClickListener { menuItem: MenuItem ->
        when(menuItem.itemId){
            R.id.menu_1->{
                Toast.makeText(this, "Menu 1 clicked", Toast.LENGTH_SHORT).show()
                true
            }
            R.id.menu_2->{
                Toast.makeText(this, "Menu 2 clicked", Toast.LENGTH_SHORT).show()
                true
            }
            else -> false
        }
    }
    
    popup.setOnDismissListener {
        // Respond to popup being dismissed.
    }
    popup.setForceShowIcon(true)
    popup.show()
}

It doesnot work on android support v7 version but you can tweak this by modifying little bit.它不适用于 android 支持 v7 版本,但您可以通过稍微修改来调整它。 below code works for me下面的代码对我有用

<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=".MainActivity">

<item
    android:id="@+id/action_settings"
    android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
    android:title="@string/action_settings"
    app:showAsAction="always">
    <menu>

        <item
            android:id="@+id/action_rate"
            android:icon="@drawable/ic_grade_black_24dp"
            android:orderInCategory="100"
            android:title="@string/action_rate"
            app:showAsAction="never" />
        <item
            android:id="@+id/action_share"
            android:icon="@drawable/ic_share_black_24dp"
            android:orderInCategory="101"
            android:title="@string/action_share"
            app:showAsAction="never" />
        <item
            android:id="@+id/action_enquiry"
            android:icon="@drawable/ic_message_black_24dp"
            android:orderInCategory="102"
            android:title="@string/action_enquiry"
            app:showAsAction="never" />
        <item
            android:id="@+id/action_disclaimer"
            android:icon="@drawable/ic_info_black_24dp"
            android:orderInCategory="103"
            android:title="@string/action_disclaimer"
            app:showAsAction="never" />
    </menu>
</item>

I found this solution: https://stackoverflow.com/a/30337653/197127 .我找到了这个解决方案: https : //stackoverflow.com/a/30337653/197127 Basically, overriding a method and it does not break the device menu button or the overflow.基本上,覆盖一个方法并且它不会破坏设备菜单按钮或溢出。 Thanks to all.谢谢大家。

Try this尝试这个

 MenuPopupHelper menuHelper = new MenuPopupHelper(getContext(), (MenuBuilder) 
 popupmenu.getMenu(), button);
 menuHelper.setForceShowIcon(true);
 menuHelper.show();

Worked for me.为我工作。

I created something like this: Try this:我创建了这样的东西:试试这个:

    Menu menu= toolbar.getMenu();
    Method menuMethod = null;
    try {
       menuMethod = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
       menuMethod.setAccessible(true);
       menuMethod.invoke(menu, true);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

From the Above 3.0 android icons in the pop menu are not displayed so you have to trick it and this works you can try从上面的 3.0 android 图标在弹出菜单中没有显示所以你必须欺骗它,这可以尝试

   //init the popup
   PopupMenu popup = new PopupMenu(context, anchor);
 
        /*  The below code in try catch is responsible to display icons*/
            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();
            }
 
        //inflate menu
        popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
 
        //show menu
        popup.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM