How to force use of overflow menu on devices with menu button

前端 未结 11 634
谎友^
谎友^ 2020-11-22 14:04

I\'d like to have all of the menu items that don\'t fit into the ActionBar go into the overflow menu (the one that is reached from the Action Bar not the menu button) ev

11条回答
  •  萌比男神i
    2020-11-22 14:32

    You can also use this little hack here:

    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) {
    }
    

    Good place to put it would be the onCreate-Method of your Application class.

    It will force the App to show the overflow menu. The menu button will still work, but it will open the menu in the top right corner.

    [Edit] Since it has come up several times now: This hack only works for the native ActionBar introduced in Android 3.0, not ActionBarSherlock. The latter uses its own internal logic to decide whether to show the overflow menu. If you use ABS, all platforms < 4.0 are handled by ABS and are thus subjected to its logic. The hack will still work for all devices with Android 4.0 or greater (you can safely ignore Android 3.x, since there aren't really any tablets out there with a menu button).

    There exists a special ForceOverflow-Theme that will force the menu in ABS, but apperently it is going to be removed in future versions due to complications.

提交回复
热议问题