2012-07-09 51 views
4

我一直在尝试创建自己的动作项目,但它对我来说不可能。造型动作项目背景ActionBarSherlock

我正在尝试做的是创建一个自定义操作项目布局(例如颜色背景),像“拇指”操作栏。我只想为我的一项活动更改行动项目。

enter image description here

我一直在玩安卓图标和android:actionLayout属性操作菜单项..但我什么也没得到。

其实,我见过的StackOverflow另一个线程,但它并没有帮助我...

Building ActionMode with custom layout in ActionBarSherlock

styling ActionbarSherlock: textColor of the action-items

任何想法?提前致谢!

+0

你可以自定义操作栏,如图所示吗? – 2013-09-18 12:16:34

+0

这是一个旧项目,我终于无法尝试。如果你可以试试它会很好!我认为下面的答案是正确的,但我还没有测试过:) 对不起,祝你好运! – cesards 2013-09-18 12:53:57

+0

该答案将改变所有行动项目的背景。我通过使用actionLayout获取菜单项。 – 2013-09-19 04:26:25

回答

12

要覆盖ActionBarSherlock主题,你应该这样进行:

开放values/abs__themes.xmlActionBarSherlock库项目。你看,例如:

<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补丁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" /> 


对于其他基本的定制,你可以用这个tool,它生成的款式适合你。

+0

感谢您的回答。帮助过我。 – Siddhesh 2013-01-25 09:00:52

+0

+1 @ Michel-F。 Portzert是否有可能在行动项目周围设置边界?如果是,该怎么办?你已经显示的步骤相同? – Roylee 2013-04-19 05:23:25

+0

我没有太多时间进行搜索,因此我建议在“drawable /”文件夹中创建一个特殊的背景xml文件。或者含有属性如果只需要一个彩色背景,或任一个如果需要的边界上的背景图像的顶部, 例如:<位图机器人:SRC =“@绘制/ ic_launcher” /> <行程机器人:宽度= “2DP” 机器人:颜色= “#FF0000”/> <固体机器人:颜色= “#00000000”/> 2013-04-19 09:36:29