基于主题更改绘制对象 [英] Change Drawable Based on Theme
问题描述
我有两种不同颜色的图像,对应于两种不同的主题,在Android中。我想用他们在我的行动吧。我想引用一个可拉伸的图标,主题无关的,但具有可拉伸参照光颜色的光的主题,和暗颜色的光的主题。
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.
在菜单:
<item
android:id="@+id/menu_attach_existing_picture"
android:icon="@drawable/buttonface_picture"
android:showAsAction="always"
android:title="@string/menu_attach_existing_picture">
</item>
然后在我想要的东西,映射@绘制/ buttonface_picture到@绘制的样式/ buttonface_picture_light在灯光主题和@可绘制/ buttonface_picture_dark为黑暗的主题。
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.
我觉得有必须是一个办法,但我无法找到的语法。如果改变任何东西,我使用ActionBarSherlock。
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.
推荐答案
好像我永远只是一个谷歌搜索或两个客场从我的答案。解决的办法是:
Seems like I'm always just a Google search or two away from my answers. The solution is:
在styles.xml
in styles.xml
<attr name="buttonface_picture" format="reference"/>
然后在实际的主题:
then in the actual theme:
<item name="buttonface_picture">@drawable/buttonface_picture_dark</item>
或
<item name="buttonface_picture">@drawable/buttonface_picture_light</item>
然后,在menu.xml文件:
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>
的<一个href="http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes">Accessing资源页面与结合本SO 最终得到了它点击我的大脑。
The Accessing Resources Page combined with this SO eventually got it to click in my brain.
这篇关于基于主题更改绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!