app:showAsAction ifRoom is not working on appcompat action bar

前端 未结 6 1990
我寻月下人不归
我寻月下人不归 2020-12-19 04:59

I have an action bar with the following menu items;



        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 05:37

    ToolBar (that is in ActivityBar) strives not to exceed some amount of visible elements. And that limit is lower than ToolBar can contain indeed. The limit is set in android.support.v7.view.ActionBarPolicy class:

    `/**
         * Returns the maximum number of action buttons that should be permitted within an action
         * bar/action mode. This will be used to determine how many showAsAction="ifRoom" items can fit.
         * "always" items can override this.
         */
        public int getMaxActionButtons() {
            final Resources res = mContext.getResources();
            final int widthDp = ConfigurationHelper.getScreenWidthDp(res);
            final int heightDp = ConfigurationHelper.getScreenHeightDp(res);
            final int smallest = ConfigurationHelper.getSmallestScreenWidthDp(res);

    if (smallest > 600 || widthDp > 600 || (widthDp > 960 && heightDp > 720) || (widthDp > 720 && heightDp > 960)) { // For values-w600dp, values-sw600dp and values-xlarge. return 5; } else if (widthDp >= 500 || (widthDp > 640 && heightDp > 480) || (widthDp > 480 && heightDp > 640)) { // For values-w500dp and values-large. return 4; } else if (widthDp >= 360) { // For values-w360dp. return 3; } else { return 2; } }`

    As you can see the limit is between 2 and 5, and it depends on the screen width. So if you want to exceed the limit, you should use showAsAction="always" or create your own view for ActionBar .

提交回复
热议问题