ActionBar MenuItem Divider

2020-01-25 00:47发布

问题:

Is there a way to show Divider between the Menu Items in ActionBar for HoneyComb+.

Some post says that the Divider will be shown only when the menu items has android:showAsAction="withText".

I want to show only the Icon not the Text.

I successfully shown Divider for Pre-HoneyComb by implementing a Action Bar Compatibility.

I dont want to use ActionBarSherlock as given in this post Android actionbar sherlok doesn't show divider because it will be time to change from Action Bar Compatibility to ActionBarSherlock in my all Projects.

When i saw the Android Source i found that Divider will be show only when it has text as shown below (from ActionMenuItemView)

public boolean needsDividerBefore() {
    return hasText() && mItemData.getIcon() == null;
}

public boolean needsDividerAfter() {
    return hasText();
}

Is there a way that I can give my Implementation for ActionMenuItemView for ActionBar where needsDividerBefore() will always give true

回答1:

I found an answer by myself with help of http://android-developers.blogspot.in/2011/04/customizing-action-bar.html However, this does not completely solve my problem. It adds a divider for Title, and also one for the home Icon. There are also left and right separators. That too is adjustable.

I added android:selectableItemBackground to my theme.

<item name="android:selectableItemBackground">@drawable/action_bar_item_selector</item>

action_bar_item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/actionbar_compat_separator"></item>

    <item android:left="1dp" android:drawable="@drawable/actionbar_compat_item"></item>

</layer-list>

actionbar_compat_separator - is my seperator drawable

and actionbar_compat_item is my selector for action bar item.

EDITED

I have found a better solution to my problem. It works well.

<item name="android:actionButtonStyle">@style/ActionButton</item> to my Theme

<style name="ActionButton" parent="android:style/Widget.Holo.Light.ActionButton">
        <item name="android:background">@drawable/action_bar_item_selector</item>
    </style>


回答2:

You can override existing theme with custom styles, for e.g.

<style name="CustomTheme" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@color/action_bar_background</item>
    <item name="android:showDividers">beginning</item>
    <item name="android:divider">@color/action_bar_divider</item>
</style>


回答3:

Update: This doesn't seem to work for Android 5 Lollipop and above:


This is the best way I found. Just add a group with a dummy item to your menu:

menu.xml

<group>
    <!--dummy item to get a nice separator-->
    <item
        android:title=""
        android:showAsAction="always"
        android:enabled="false" />
</group>

<item android:id="@+id/action_example"
    ...

The dummy item has an empty title so it appears invisible, and it's disabled so it can't be clicked.