Android的自定义窗口小部件的样式:如何把它们放进一个名字空间?放进、自定义、部件、样式

2023-09-06 01:44:45 作者:Robert 光辉和著名的

在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.

推荐答案

我有导致的错误消息类似的问题的未找到属性在包资源标识符

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"
/>