Android custom widget styles: how to put them into a namespace?(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 自定义小部件样式:如何将它们放入命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 自定义小部件样式:如何将它们放入命名空间?


基础教程推荐
- :hover 状态不会在 iOS 上结束 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01