根据主题更改 Drawable
Posted
技术标签:
【中文标题】根据主题更改 Drawable【英文标题】:Change Drawable Based on Theme 【发布时间】:2012-11-24 00:19:10 【问题描述】:我有两种不同颜色的图像,对应于 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。
【问题讨论】:
【参考方案1】:似乎我总是离我的答案只有一两个谷歌搜索。解决办法是:
在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 最终让它在我的脑海中点击。
【讨论】:
以编程方式制作怎么样? 该特定答案给出了 attr -> R 标识符 -> 维度的示例。您必须改为使用该标识符来访问可绘制对象 @AlexS 试试drawable.applyTheme(view.context.theme)
以上是关于根据主题更改 Drawable的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Qt C++ 中根据主题更改图标?如果可用的主题是深色或浅色