样式化操作项背景ActionBarSherlock

4
我一直在尝试创建自己的操作项,但对我来说是不可能的。
我想要做的是创建一个自定义的操作项布局(例如彩色背景),类似于“Thumbs”操作栏。我只想更改一个活动中的操作项。

enter image description here

我一直在尝试使用android:icon和android:actionLayout属性来设置操作菜单项目,但是没有成功。
实际上,我在StackOverflow上看到了其他的帖子,但是对我没有帮助... 使用ActionBarSherlock构建自定义布局的ActionMode 设置ActionbarSherlock的样式:操作项的文本颜色 有什么想法吗?提前感谢!

你能像图片中所示一样自定义ActionBar吗? - subair_a
这是一个旧项目,我最终无法尝试它。如果你能试一下就好了!我认为下面的答案是正确的,但我还没有测试过 :)抱歉并祝好运! - cesards
那个答案将会改变所有操作项的背景。我通过为菜单项使用actionLayout使其工作。 - subair_a
1个回答

12

如果要覆盖 ActionBarSherlock 的主题,您应该按照以下步骤进行:

ActionBarSherlock 库项目中打开 values/abs__themes.xml。您将看到类似以下内容的代码:

<style name="Theme.Sherlock" parent="Sherlock.__Theme">
    <!-- Action bar styles (from Theme.Holo) -->
    <item name="actionDropDownStyle">@style/Widget.Sherlock.Spinner.DropDown.ActionBar</item>
    <item name="actionButtonStyle">@style/Widget.Sherlock.ActionButton</item>
    <item name="actionOverflowButtonStyle">@style/Widget.Sherlock.ActionButton.Overflow</item>
    <item name="actionModeBackground">@drawable/abs__cab_background_top_holo_dark</item>
    <item name="actionModeSplitBackground">@drawable/abs__cab_background_bottom_holo_dark</item>
    <item name="actionModeCloseDrawable">@drawable/abs__ic_cab_done_holo_dark</item>
    <item name="actionBarTabStyle">@style/Widget.Sherlock.ActionBar.TabView</item>
    ...
    // Here is what you wanted
    <item name="actionBarItemBackground">@drawable/abs__item_background_holo_dark</item>
    ...

当你找到想要自定义的项目(在你的情况下是actionBarItemBackground),你需要在你的项目内创建一个名为themes.xml的文件,并添加以下内容:

<style name="Custom.Theme.Sherlock" parent="@style/Theme.Sherlock">
    <item name="actionBarItemBackground">@drawable/my__item_background_holo_dark</item>
</style>

这将覆盖默认的Theme.Sherlock主题,设置自定义的actionBarItemBackground

现在,您应该在活动中使用setTheme(R.style.Custom_Theme_Sherlock)而不是使用Theme.Sherlock。您可能还想覆盖另外两个主题 (Theme.Sherlock.LightTheme.Sherlock.Light.DarkActionBar)。

还有一个提示,这是ActionBarSherlock用于默认操作项背景(在holo_light下)的可绘制选择器,它使用9-patch png可绘制对象:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/abs__list_selector_disabled_holo_light" />
<item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/abs__list_selector_disabled_holo_light" />
<item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/abs__list_selector_background_transition_holo_light" />
<item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/abs__list_selector_background_transition_holo_light" />
<item android:state_focused="true"                                                             android:drawable="@drawable/abs__list_focused_holo" />
<item                                                                                          android:drawable="@android:color/transparent" />

如果你想进行其他基本的自定义操作,你可以使用这个工具,它会为你生成样式。


+1 @Michel-F. Portzert,是否可以在操作项周围加上边框?如果可以,应该如何实现?与您展示的步骤相同吗? - Roy Lee
我没有太多时间去搜索,所以我建议在你的“drawable/”文件夹中创建一个特殊的背景xml文件。如果你只需要一个有颜色的背景,那么可以包含一个带有<stroke>和<color>属性的<shape>,或者如果你需要在背景图像上面加边框,则可以使用<layer-list>。例如:<layer-list><item ><bitmap android:src="@drawable/ic_launcher"/> </item><item ><shape > <stroke android:width="2dp" android:color="#FF0000"/> <solid android:color="#00000000"/> </shape></item></layer-list> - Michel-F. Portzert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接