Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

android - Change ActionBar Menu Icons depending on Style

I want to use different ActionBar Icons depending on which style I use (Dark or Light). I couldn't figure it out how, heres what I tried:

<style name="AppThemeDark" parent="Theme.Sherlock.ForceOverflow">
    <item name="android:actionButtonStyle">@style/customActionButtonStyleDark</item>
    <item name="actionButtonStyle">@style/customActionButtonStyleDark</item>
</style>

<style name="AppThemeLight" parent="Theme.Sherlock.Light.ForceOverflow">
    <item name="android:actionButtonStyle">@style/customActionButtonStyleLight</item>
    <item name="actionButtonStyle">@style/customActionButtonStyleLight</item>
</style>

<style name="customActionButtonStyleDark" >
    <item name="@drawable/action_search">@drawable/action_search</item>
</style>

<style name="customActionButtonStyleLight" >
    <item name="@drawable/action_search">@drawable/action_search_light</item>
</style>

I also tried to insert <item name="@drawable/action_search">@drawable/action_search</item> directly into the theme style, but nothing worked.
Any ideas how?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Even though you found a workaround, maybe this will help someone else. I found a simple way to do this in xml (what logcat's answer is referring to). The trick I used was to create custom attributes for my menu/actionbar icons. You have to have one attribute per menu item icon.

You need to create attrs.xml in your values folder, and add your custom attributes. Think of each attribute as a constant that your themes/styles set, and then your styles/views can use those contants to set properties.

<declare-styleable name="customAttrs">
    <attr name="customSearchIcon" format="reference" />
</declare-styleable>

In your styles.xml in your values folder, have your themes/styles that set your custom icon attributes to drawable references.

<style name="AppThemeDark" parent="Theme.Sherlock.ForceOverflow">
    <item name="customSearchIcon">@drawable/action_search</item>
</style>

<style name="AppThemeLight" parent="Theme.Sherlock.Light.ForceOverflow">
    <item name="customSearchIcon">@drawable/action_search_light</item>
</style>

Lastly, in your [menu_name].xml in your menu folder, have your menu item set its icon to your matching custom icon attribute.

<item
    android:id="@+id/menuitem_search" 
    android:icon="?attr/customSearchIcon"/>

Now, depending on what theme is set, the icon for the menu item will change. Also, this allows you to still have API specific versions of your drawables (light and dark) using resource identifiers with your drawable folders, so you can have different menu style icons for pre 3.0 and actionbar style icons for 3.0+.

Also, remember when setting a theme at runtime (vs AndroidManifest.xml), you must set it before calling setContentView() in your Activity. It is recommended to restart your activity after changing the theme of an Activity that has already been created.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...