如何在 ActionBar 的溢出菜单中显示图标 [英] How To show icons in Overflow menu in ActionBar

查看:26
本文介绍了如何在 ActionBar 的溢出菜单中显示图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道无法使用本机 API.是否有实现这种视图的解决方法?

I know it's not possible using the native API. Is there a workaround to implement that kind of view?

推荐答案

之前贴的答案还可以,一般来说.但它基本上删除了溢出菜单的默认行为.诸如可以在不同的屏幕尺寸上显示多少个图标,然后在无法显示时将它们放入溢出菜单之类的事情.通过执行上述操作,您可以删除许多重要的功能.

The previously posted answer is OK, generally speaking. But it basically removes the default behaviour of the Overflow menu. Things like how many icons can be displayed on different screen-sizes and then they dropped off into the overflow menu when they can't be displayed. By doing the above you remove a lot of important functionality.

更好的方法是让溢出菜单直接显示图标.您可以通过将以下代码添加到您的活动中来实现.

A better method would be to tell the overflow menu to display the icons directly. You can do this by adding the following code to your Activity.

@Override
public boolean onMenuOpened(int featureId, Menu menu)
{
    if(featureId == Window.FEATURE_ACTION_BAR && 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(NoSuchMethodException e){
                Log.e(TAG, "onMenuOpened", e);
            }
            catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}

这篇关于如何在 ActionBar 的溢出菜单中显示图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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