ActionBar capacity/overflow not changing on orientation change

╄→尐↘猪︶ㄣ 提交于 2019-12-03 00:18:54

I ran into this problem to and came up with a hack which works 100%.

I eneded up calling invalidateOptionsMenu() two times. Look at the code below:

private boolean hackActionBarReset = false;

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    //a hack to reset the items in the action bar.
    hackActionBarReset = true;
    invalidateOptionsMenu();
    hackActionBarReset = false;
    invalidateOptionsMenu();

}

And in your onCreateOptionsMenu() set this on you menuitems you want to show:

item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT | (hackActionBarReset ? MenuItem.SHOW_AS_ACTION_NEVER : MenuItem.SHOW_AS_ACTION_IF_ROOM));

Not a pretty solution but it works.

I've cautiously worked around this: when the device's width is greater than 850dip, force showing all items in the ActionBar, otherwise continue to let the platform decide.

Here's the git commit. Edit: and the follow-up commit to fix using a field that's too new, oops. :-)

I'm definitely still interested in better answers (other than waiting for a fix to the platform).

I found that the simplest and most effective workaround was to clear the menu and then rebuild it.

For example:

Menu menu;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    // Setup code goes here...
}

@Override
public void onOrientationChanged() {
    if (menu != null) {
        menu.close(); // Ensure the menu is closed before changing its contents.
        menu.clear();
        onCreateOptionsMenu(menu); // Rebuild the menu.
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!