Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - Change Drawable Based on Theme

I've got an image in two different colors, corresponding to two different themes in Android. I want to use them in my Action Bar. I want to refer to a drawable for the icon, theme agnostic, but have that drawable reference the light color for the light theme, and the dark color for the light theme.

In the menu:

    <item
        android:id="@+id/menu_attach_existing_picture"
        android:icon="@drawable/buttonface_picture"
        android:showAsAction="always"
        android:title="@string/menu_attach_existing_picture">
    </item>

Then in the styles I want to have something that maps @drawable/buttonface_picture to @drawable/buttonface_picture_light in the light theme and @drawable/buttonface_picture_dark for the dark theme.

I feel like there's got to be a way, but I'm having trouble finding the syntax. If it changes anything, I am using ActionBarSherlock.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Seems like I'm always just a Google search or two away from my answers. The solution is:

in styles.xml

<attr name="buttonface_picture" format="reference"/>

then in the actual theme:

<item name="buttonface_picture">@drawable/buttonface_picture_dark</item>

or

<item name="buttonface_picture">@drawable/buttonface_picture_light</item>

Then, in the menu.xml:

<item
    android:id="@+id/menu_attach_existing_picture"
    android:icon="?attr/buttonface_picture"
    android:showAsAction="always"
    android:title="@string/menu_attach_existing_picture">
</item>

The Accessing Resources Page combined with this SO eventually got it to click in my brain.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...