Styling action item background ActionBarSherlock

拥有回忆 提交于 2019-12-09 13:24:52

问题


I've been trying to create my own action item, but it has benn impossible for me.

What I'm tryind to do, is creating a custom action item layout (color background for example), something like "Thumbs" action bar. I only want to change action items only for one of my activities.

I've been playing with android:icon and android:actionLayout property for action menu items.. but I got nothing.

Actually, I've seen another threads in StackOverflow, but it didn't help me ...

Building ActionMode with custom layout in ActionBarSherlock

styling ActionbarSherlock: textColor of the action-items

Any ideas? Thanks in advance!


回答1:


To override ActionBarSherlock themes you should proceed like this:

Open values/abs__themes.xml from ActionBarSherlock library project. You see for example:

<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>
    ...

When you have found the item you want to customize (actionBarItemBackground in your case), you create your own themes.xml inside your project, and add in it:

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

This overrides the default Theme.Sherlock, setting a custom actionBarItemBackground.

Now, instead of using Theme.Sherlock in your activity you should use setTheme(R.style.Custom_Theme_Sherlock). You may also want to override the other two themes (Theme.Sherlock.Light and Theme.Sherlock.Light.DarkActionBar)

One more tip, here is the drawable selector used by ActionBarSherlock for the default action item background (in holo_light), it uses 9-patch png drawables:

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


For other basic customizing, you can use this tool, it generates styles for you.



来源:https://stackoverflow.com/questions/11397407/styling-action-item-background-actionbarsherlock

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