Is there a standard way to add dividers between action bar items in Android 3.0?

走远了吗. 提交于 2019-11-27 01:23:36

问题


I have a slight problem trying to customise the look of the action bar in my app. I want to be able to have the pixel wide dividers to group action bar items that you see in many of the native apps (e.g. Gmail, Calendar). I found a way to do this by adding a menu item and setting the 'android:actionLayout' attribute to a custom layout for the divider:

<View
  android:background="@color/LightGray"
  android:layout_marginTop="5dip"
  android:layout_marginBottom="5dip"
  android:layout_width="1dip"
  android:layout_height="fill_parent" />

This works nicely, but the issue is it counts as a menu item and the action bar seems to limit the number of menu items to 4 - any others get pushed into the overflow menu.

So I guess what I'm asking is whether there is a standard way to add item dividers without having to use a menu item with a custom view, and in a way that doesn't count towards the limit for action bar items?

Thanks in advance!


回答1:


I wouldn't try and force dividers into places that the system does not add them automatically as it will make your app inconsistent with the platform. The default behavior is:

  • Divider between overflow and others.
  • Divider between text and another item where it would disambiguate which item the text belongs to.



回答2:


I couldn't find a standard way, but the way I did it was to use the android:actionLayout property for the menu item, and I put the divider in there.




回答3:


When Google released the 3.0 SDK I got a quick demo app to see how the ActionBar works and just looking back at it, if I use Text Items without Icon drawables, then I get automatic dividers drawn.

My menu.xml file is like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/menu_text" android:showAsAction="ifRoom" android:title="@string/action_label_text" />
  <item android:id="@+id/menu_text" android:showAsAction="ifRoom" android:title="@string/action_label_text" />
  <item android:id="@+id/menu_text" android:showAsAction="ifRoom" android:title="@string/action_label_text" />
  <item android:id="@+id/menu_text" android:showAsAction="ifRoom" android:title="@string/action_label_text" />
</menu>

Maybe this won't work with icons??

Or thinking about it, maybe the size of the icon has an effect?



来源:https://stackoverflow.com/questions/5910133/is-there-a-standard-way-to-add-dividers-between-action-bar-items-in-android-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!