android - 根据主题更改 Drawable

标签 android styles

我有两种不同颜色的图像,对应于 Android 中的两个不同主题。我想在我的操作栏中使用它们。我想为图标引用可绘制对象,与主题无关,但让该可绘制对象引用浅色主题的浅色和浅色主题的深色。

在菜单中:

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

然后在样式中,我想要将 @drawable/buttonface_picture 映射到浅色主题中的 @drawable/buttonface_picture_light 和深色主题中的 @drawable/buttonface_picture_dark。

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

最佳答案

似乎我总是离我的答案只有一两个谷歌搜索。解决办法是:

在styles.xml中

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

然后在实际主题中:

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

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

然后,在 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>

Accessing Resources Page结合 this SO最终让它在我的脑海中点击。

关于android - 根据主题更改 Drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13735675/

相关文章:

javascript - 是否只有在尚未安装应用程序的情况下才能为移动设备浏览器启动弹出窗口?

.net - WPF:为什么这会导致堆栈溢出异常?

android - 如何继承AlertDialog按钮的样式

c# - 找出风格问题的原因

wpf - 将相同样式应用于多个元素

java - 使用自定义适配器将项目动态添加到 ListView 中的问题

android - iOS 通知负载中是否需要 aps 字典?我们可以为 iOS 和 Android 使用相同的有效负载吗?

java - 带有俄语网址的 ImageView

android - Xmlapter 示例不工作。包含(联系人阅读器、Rss 提要、照片显示)

java - 更改在colors.xml中设置的styles.xml中的颜色