在工具栏溢出菜单中显示菜单项图标时,这种奇怪的情况是如何发生的? [英] How does this strange condition happens when show menu item icon in toolbar overflow menu?

查看:24
本文介绍了在工具栏溢出菜单中显示菜单项图标时,这种奇怪的情况是如何发生的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在工具栏中显示一个溢出菜单(AppCompat-v7:22.1.1),下面是我的 menu_main.xml.

<项目android:id="@+id/action_search";android:title="@string/action_search";android:icon=@mipmap/ic_menu_search"android:orderInCategory="100";android:actionViewClass=android.widget.SearchView"app:showAsAction="ifRoom"/><项目android:id=@+id/menu_group_chat"android:title="@string/menu_group_chat";android:icon=@mipmap/ic_menu_groupchat"/><项目android:id="@+id/menu_add_friend";android:title="@string/menu_add_friend";android:icon=@mipmap/ic_menu_add_friend"/>

运行我的应用程序后,没有显示菜单项的图标,然后我尝试了这个 ,我知道AppCompatActivity.onMenuOpened 在 22.x 中不再被调用,但奇怪的是,当我在 Genymotion 中单击硬件菜单键时,菜单出现在底部并带有图标,

关闭菜单后,我再次点击工具栏中的溢出按钮,菜单中出现这些图标,

好奇怪啊!为什么会发生这种情况?

解决方案

对于 AppCompactActivity 你可以把这个检查放在 onPrepareOptionsPanel() 上.

@Overrideprotected boolean onPrepareOptionsPanel(视图视图,菜单菜单){如果(菜单!= null){if (menu.getClass().getSimpleName().equals("MenuBuilder")) {尝试 {方法 m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);m.setAccessible(true);m.invoke(菜单,真);} 捕获(异常 e){Log.e(getClass().getSimpleName(), "onMenuOpened...无法为溢出菜单设置图标", e);}}}返回 super.onPrepareOptionsPanel(view, menu);}

I want to show a overflow menu in toolbar(AppCompat-v7:22.1.1), below is my menu_main.xml.

<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_search"
    android:title="@string/action_search"
    android:icon="@mipmap/ic_menu_search"
    android:orderInCategory="100"
    android:actionViewClass="android.widget.SearchView"
    app:showAsAction="ifRoom"/>

<item
    android:id="@+id/menu_group_chat"
    android:title="@string/menu_group_chat"
    android:icon="@mipmap/ic_menu_groupchat" />

<item
    android:id="@+id/menu_add_friend"
    android:title="@string/menu_add_friend"
    android:icon="@mipmap/ic_menu_add_friend" />

After running my app, the icon of menu item is not displayed, then I tried this solution, add an override method onMenuOpened() in my Activty(extends from AppCompatActivity),

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if(menu!=null){
        if(menu.getClass().getSimpleName().equals("MenuBuilder")){
            try {
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}

But after running this demo, I find that the icon is still not displayed.

From this reported issue, I know that AppCompatActivity.onMenuOpened is not called any more in 22.x, but it's odd that when I click the hardware menu key in Genymotion, the menu appear at the bottom and with the icon,

after closing the menu, I click the overflow button in the toolbar again, these icons in menu appear,

how strange it is! Why this happens?

解决方案

For the AppCompactActivity you can put this check on the onPrepareOptionsPanel() instead.

@Override
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
                try {
                    Method m = menu.getClass().getDeclaredMethod(
                            "setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                    Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e);
                }
            }
        }
    return super.onPrepareOptionsPanel(view, menu);
}

这篇关于在工具栏溢出菜单中显示菜单项图标时,这种奇怪的情况是如何发生的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆