如何创建自己的资源名称? [英] How do I create my own resource names?
问题描述
我可以使用的themes.xml
我自己的资源名称?例如:
Can I use my own resource names in themes.xml
? For example:
<item name="headerBackground">@drawable/header</item>
在这个例子中, headerBackground
不是一个有效的资源(未发现的资源匹配给定的名称...
说的Eclipse)。
In this example, headerBackground
is not a valid resource (No resource found that matches the given name...
, says Eclipse).
我应该在哪里创建/声明这些资源呢?这是一个坏主意?
Where should I create/declare these resources? Is this a bad idea?
推荐答案
当然,你可以用你自己的名字,否则你会被卡住了Android默认可绘制..:)
Of course you can use your own names, otherwise you'd be stuck with the Android default drawables.. :)
您通过将文件与该名称在 RES /绘制
目录申报绘制的名字。
You declare drawable names by placing a file with that name in your res/drawable
directory.
更新,实际的回答:
您需要声明一个设置样式属性,在 RES /价值/ attrs.xml
(实际文件名没有关系,但是这似乎是惯例):
You need to declare a styleable attribute, in res/values/attrs.xml
(the actual filename doesn't matter, but this seems to be the convention):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyCustomView">
<attr name="headerBackground" format="reference" />
</declare-styleable>
</resources>
然后就可以值在主题中分配给它,例如:
Then you can assign it a value in your theme, for example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
<item name="headerBackground">@drawable/header</item>
</style>
</resources>
当然,无论查看
你实现使用这个新定义的属性必须能够加载该绘制对象
并应用它。该<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Gallery1.html"相对=nofollow>图库例如的对Android开发者网站是有有益的。
Of course whatever View
you're implementing to use this newly-defined attribute has to be able to load that Drawable
and apply it. The Gallery example on the Android Developers' site is useful there.
这篇关于如何创建自己的资源名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!