基于主题改变Drawable

fre*_*ing 30 android styles

我有两种不同颜色的图像,对应Android中的两个不同主题.我想在我的Action Bar中使用它们.我想参考一个drawable的图标,主题不可知,但有可绘制的参考灯光主题的浅色和浅色主题的深色.

在菜单中:

    <item
        android:id="@+id/menu_attach_existing_picture"
        android:icon="@drawable/buttonface_picture"
        android:showAsAction="always"
        android:title="@string/menu_attach_existing_picture">
    </item>
Run Code Online (Sandbox Code Playgroud)

然后在风格中我希望在light主题中将@ drawable/buttonface_picture映射到@ drawable/buttonface_picture_light,为黑暗主题设置@ drawable/buttonface_picture_dark.

我觉得必须有办法,但我找不到语法.如果它改变了什么,我正在使用ActionBarSherlock.

fre*_*ing 83

似乎我总是只是谷歌搜索或两个远离我的答案.解决方案是:

在styles.xml中

<attr name="buttonface_picture" format="reference"/>
Run Code Online (Sandbox Code Playgroud)

那么在实际主题中:

<item name="buttonface_picture">@drawable/buttonface_picture_dark</item>
Run Code Online (Sandbox Code Playgroud)

要么

<item name="buttonface_picture">@drawable/buttonface_picture_light</item>
Run Code Online (Sandbox Code Playgroud)

然后,在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>
Run Code Online (Sandbox Code Playgroud)

访问资源页面联合该SO最终得到了它在我的脑子点击.