How to control ActionBar split programmatically?

2019-01-22 02:39发布

The android ActionBar may split into a top and bottom bars if activity's parameter "uiOptions" is set to "splitActionBarWhenNarrow", note this parameter is only valid in ICS.

Honeycomb has introduced a new approach to multi-select list items using action bar. When a item is under press & hold the list becomes into a multi-selection mode and the actionbar bar may be used to accomplish some actions. The actionbar setup is inherited from the list activity, i.e., if the activity has a split action bar the multi-selection will have too, and if the activity has only the top bar, so, the multi-selection will be compliant with that.

The question is, is it possible to have only a top action bar in the activity and when the list turns into multi-selection mode programmatically split the actionbar?

Thanks!

5条回答
欢心
2楼-- · 2019-01-22 03:24

No, you cannot switch between split and non-split action bars on the fly.

The setter counterpart to android:uiOptions is on Window, not Activity. Window#setUiOptions is the method and the flag to use is ActivityInfo#UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW.

However this isn't going to do what you want. Split action bar when narrow must be specified as the window is first configured before the window decor is initialized. In other words, once the window has been displayed (or even once you've called setContentView) it's too late to change it.

This was a conscious decision by the Android UX team. Action modes (including selection modes) are meant to mirror the configuration of the action bar on the current activity. This gives the user a single place to look for currently valid actions within the same activity.

查看更多
虎瘦雄心在
3楼-- · 2019-01-22 03:24

Can you try to set getWindow().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW) in the Activity you want the ActionBar to show as split right before its onCreate(), then hide it using getActionBar.hide() and on the list action you are referring to make it pop-up back using getActionBar.show().

I tried (not with the support libraries though) showing ActionBar in one Activity and Split in another using the above and was able to hide and show the split on button clicks. Hope this helps. Let me know in case it did or even didn't. Happy coding. :)

查看更多
可以哭但决不认输i
4楼-- · 2019-01-22 03:37

The AppCompat package now offers the Toolbar widget that allows you to put an actionbar anywhere you want in your layout and modify it like any other view..

Please see the full documentation here and a guide here.

查看更多
够拽才男人
5楼-- · 2019-01-22 03:40

I don't believe so. I do not see anything in Activity that would serve as a setter counterpart to android:uiOptions.

查看更多
SAY GOODBYE
6楼-- · 2019-01-22 03:40

You can use two Toolbars. Have a look at these questions:

AppCompat v7:21 Split Action Bar Broken?

How to center action menu on toolbar

查看更多
登录 后发表回答