如何在带有菜单按钮的设备上强制使用溢出菜单

161

我希望所有无法适应ActionBar的菜单项都进入溢出菜单(从ActionBar而不是菜单按钮中访问),即使在菜单按钮的设备上也是如此。对用户来说,这似乎比将它们抛到需要用户从触摸(屏幕)交互跳转到基于按钮的交互的单独菜单列表中要更直观。

在模拟器中,我可以将“硬件返回/主页键”值设置为“否”,以获得此效果。 我已经搜索了一种在具有菜单按钮但找不到的实际设备上执行此操作的方法。 有人能帮我吗?

12个回答

0

对于使用新的工具栏的任何人:

private Toolbar mToolbar;

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    ...
}


@Override
public boolean onKeyUp(int keycode, KeyEvent e) {
    switch(keycode) {
        case KeyEvent.KEYCODE_MENU:
            mToolbar.showOverflowMenu();
            return true;
        }

    return super.onKeyUp(keycode, e);
}

0
try {
    ViewConfiguration config = ViewConfiguration.get(this);
    Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
    if (menuKeyField != null) {
        menuKeyField.setAccessible(true);
        menuKeyField.setBoolean(config, false);
    }
} catch (Exception ignored) {
}

当升级到Android 14时,此部分不再使用(因为它属于非SDK支持)。有了这样的逻辑,我该如何转换呢?请问是否有人已经做过,可以指导我一下。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接