Android 操作栏菜单项分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11338867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActionBar MenuItem Divider
提问by Vivek Khandelwal
Is there a way to show Divider between the Menu Items in ActionBar for HoneyComb+.
有没有办法在 HoneyComb+ 的 ActionBar 中的菜单项之间显示分隔线。
Some post says that the Divider will be shown only when the menu items has android:showAsAction="withText".
一些帖子说只有当菜单项具有 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.
我通过实现 Action Bar Compatibility 成功地展示了 Pre-HoneyComb 的 Divider。
I dont want to use ActionBarSherlock as given in this post Android actionbar sherlok doesn't show dividerbecause it will be time to change from Action Bar Compatibility to ActionBarSherlock in my all Projects.
我不想使用这篇文章中给出的 ActionBarSherlock Android actionbar sherlok 不显示分隔符,因为是时候在我的所有项目中从 Action Bar Compatibility 更改为 ActionBarSherlock 了。
When i saw the Android Source i found that Divider will be show only when it has text as shown below (from ActionMenuItemView)
当我看到 Android Source 时,我发现 Divider 只有当它有如下所示的文本时才会显示(来自 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
有没有一种方法可以为 ActionBar 提供 ActionMenuItemView 的实现,其中 needDividerBefore() 将始终为 true
采纳答案by Vivek Khandelwal
I found an answer by myself with help of http://android-developers.blogspot.in/2011/04/customizing-action-bar.htmlHowever, 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.
我在http://android-developers.blogspot.in/2011/04/customizing-action-bar.html 的帮助下自己找到了答案,但是,这并不能完全解决我的问题。它为标题添加了一个分隔符,也为主页图标添加了一个分隔符。还有左右分隔符。这也是可调的。
I added android:selectableItemBackground to my theme.
我将 android:selectableItemBackground 添加到我的主题中。
<item name="android:selectableItemBackground">@drawable/action_bar_item_selector</item>
action_bar_item_selector.xml
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
actionbar_compat_separator - 我的分隔符是可绘制的
and actionbar_compat_item is my selector for action bar item.
和 actionbar_compat_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>
回答by Pawan Maheshwari
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>
回答by Aphex
Update: This doesn't seem to work for Android 5 Lollipop and above:
更新:这似乎不适用于 Android 5 Lollipop 及更高版本:
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.
虚拟项目的标题为空,因此看起来不可见,并且已禁用,因此无法单击。