制作资源主题依赖 [英] Making resources theme dependent
问题描述
现在,我们有两个图标(黑暗与光明),如<一个描述href="http://developer.android.com/guide/practices/ui_guidelines/icon_design_action_bar.html">ActionBar图标指南。
Now that we have two Icons (Dark and Light) as described in ActionBar Icon Guide.
@drawable/ic_search_light
@drawable/ic_search_dark
如何参考这些图标中的XML菜单资源:
How to reference these Icons in XML menu resource:
&LT;项目的android:标题=搜索安卓图标=
哪些可绘制在这里?
/&GT;
<item android:title="Search" android:icon="
Which drawable here ?
"/>
光明和黑暗之间的每一次开关应用的主题,我必须更新所有这些绘制参考?
Every time switch Application theme between Light and Dark, Do I have to update all these drawable references ?
推荐答案
有一种方法来定义机器人的可绘制的(和 RES /值发现了许多其他元素的),以是主题相关。
There's a way to define android drawables (and many other elements found in res/values) to be Theme dependent.
让假设我们有两个的可绘的,在这种情况下,菜单图标
Lets suppose we have two drawables, menu icons in this case:
res/drawable/ic_search_light.png
res/drawable/ic_search_dark.png
和我们要使用 ic_search_dark.png
的应用程序的主题是默认的主题
或扩展它,同样,我们希望 ic_search_light.png
如果我们的应用主题更改默认 Theme.Light
或某些主题的延伸吧。
And we want to use ic_search_dark.png
for Application theme which is default Theme
or extends it, Similarly, we want ic_search_light.png
if our Application theme changes to default Theme.Light
or some theme extending it.
定义的一般属性,在一个独特的名字的 /res/attrs.xml 的,如:
Define a general attribute with a unique name in /res/attrs.xml like:
<resources>
<attr name="theme_dependent_icon" format="reference"/>
</resources>
这是一个全球性的属性和格式类型为基准,如果是自定义视图可以与风格,能够属性来定义的:
This is a global attribute and format type is reference, In case of a Custom View it can be defined along with style-able attributes:
<resources>
<declare-styleable name="custom_menu">
<attr name="theme_dependent_icon" format="reference"/>
</declare-styleable>
</resources>
其次,定义两个主题,延伸默认主题
和 Theme.Light
(或主题,从这些继承)在 RES / styles.xml 或 RES /的themes.xml 的:
Next, define two Themes, extending default Theme
and Theme.Light
(or themes that inherit from these) in res/styles.xml or res/themes.xml:
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="theme_dependent_icon" >@drawable/ic_search_dark</item>
</style>
<style name="CustomTheme.Light" parent="android:Theme.Light">
<item name="theme_dependent_icon" >@drawable/ic_search_light</item>
</style>
</resources>
最后,使用我们定义将这些图标的参考属性。在这种情况下,我们使用同时限定一个菜单布局
Finally, use the reference attribute we define to refer to these icons. In this case, we use while defining a menu layout
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Menu Item" android:icon="?attr/theme_dependent_icon"/>
</menu>
?ATTR
指的是当前主题中使用的属性。
?attr
refers to an attribute of current theme in use.
现在,我们可以用上面的两个主题的应用程序:
Now, we can use above two themes for application:
<application android:theme="@style/CustomTheme">
或
<application android:theme="@style/CustomTheme.Light">
和相应的资源将相应使用。
and corresponding resources will be used accordingly.
主题,也可以在code应用,通过在一开始就设置它的活动的的onCreate()
。
Theme can also be applied in Code, by setting it at the very beginning of Activity's onCreate()
.
更新
方法来访问code这些主题相关的资源在解释这个答案。
Method to access these theme dependent resources from code is explained in this answer.
这篇关于制作资源主题依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!