How to always display the overflow menu in android

In Android programming, generally speaking, when there are too many items in the Actionbar, a menu with three vertical dots will be displayed, but it is found that it is not displayed during the real machine test. After searching for information and testing, the problem is found: if The machine has a physical menu button, and instead of displaying the overflow menu on the right, press menu to generate it instead. This is not conducive to a unified interface style.

We can change the display of this by changing the system to detect the presence or absence of the entity's menu key.

The menu display is judged according to the public boolean hasPermanentMenuKey () method. This method is to get the boolean value of sHasPermanentMenuKey.

The workaround is as follows:

Add in onCreate():


       super.onCreate (savedInstanceState);
        makeActionOverflowMenuShown();
        setContentView(R.layout.activity_android);

    /**
     * Some phones do not display the menu bar
     */
    private void makeActionOverflowMenuShown() {
        //devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {

        }
    }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326120335&siteId=291194637