使资源主题依赖 [英] Making resources theme dependent

查看:14
本文介绍了使资源主题依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我们有两个图标(暗和亮),如 ActionBar Icon指南.

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:

<item android:title="Search" android:icon="这里可以绘制哪个?"/>

每次在 Light 和 Dark 之间切换应用主题时,我是否必须更新所有这些可绘制引用?

Every time switch Application theme between Light and Dark, Do I have to update all these drawable references ?

推荐答案

有一种方法可以将 android drawables(以及 res/values 中的许多其他元素)定义为取决于主题.

There's a way to define android drawables (and many other elements found in res/values) to be Theme dependent.

假设我们有两个 drawable,在本例中为菜单图标:

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 作为默认 Theme 的应用程序主题或扩展它,同样,我们想要 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>

接下来,定义两个 Themes,在 res/styles.xmlTheme 和 Theme.Light(或从它们继承的主题)> 或 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.

Theme 也可以应用在 Code 中,通过在 Activity 的 onCreate() 的最开始设置它.

Theme can also be applied in Code, by setting it at the very beginning of Activity's onCreate().

更新

从代码访问这些主题相关资源的方法在这个答案中进行了说明.

Method to access these theme dependent resources from code is explained in this answer.

这篇关于使资源主题依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆