Android 自定义小部件样式:如何将它们放入命名空间? [英] Android custom widget styles: how to put them into a namespace?

查看:12
本文介绍了Android 自定义小部件样式:如何将它们放入命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ApiDemos 中有一个名为 Gallery1 的视图示例,它在 attrs.xml 中声明了一个自定义样式,如下所示:

In the ApiDemos, there is a view example called Gallery1 which declares a custom style in attrs.xml, as such:

<declare-styleable name="Gallery1">
    <attr name="android:galleryItemBackground" />
</declare-styleable>

现在,我想为我的小部件做同样的事情,但使用不同的命名空间.但是,一旦我用其他东西替换了 android: 命名空间,我就会收到这个错误:

now, I want to do the same thing for my widgets, but using a different namespace. However, as soon as I replace the android: namespace with something else, I get this error:

错误:在 Gallery1 中,找不到属性 myns:galleryItemBackground

ERROR: In Gallery1, unable to find attribute myns:galleryItemBackground

找不到属性?为什么它会寻找我要声明的属性?这个文件的重点不就是能够命名您自己的自定义属性吗?

Unable to find attribute? Why does it look for an attribute I am about to declare? Isn't the point of this file to be able to name your own custom attributes?

有趣的是,如果您不提供自定义命名空间,而只提供属性名称,它会起作用.

It's interesting to note that it works if you do not supply a custom namespace, but just an attribute name.

推荐答案

我遇到了类似的问题导致错误消息No resource identifier found for attribute in package

I had a similar problem resulting in the error message No resource identifier found for attribute in package

我的解决方案是在使用自定义属性时声明命名空间.

The solution for me was to declare the namespace when you use the custom attribute.

在您使用自定义属性的 xml 文件中指定:

In your xml file where you use your custom attribute specify:

xmlns:myns="http://schemas.android.com/apk/res-auto"

...

<gallery.widget.package.Gallery1


myns:myCustomAttr="xxx"
/>

这篇关于Android 自定义小部件样式:如何将它们放入命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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