style.xml 中的自定义属性 [英] Custom attributes in styles.xml
问题描述
我创建了一个自定义小部件,并在 layout.xml 中声明它.我还在 attr.xml 中添加了一些自定义属性.但是,当尝试在styles.xml 中的样式中声明这些属性时,它给了我找不到与给定名称匹配的资源:attr 'custom:attribute'.
I have created a custom widget, and I'm declaring it in layout.xml. I have also added some custom attributes in attr.xml. However, when trying to declare these attributes in a style in styles.xml, it's giving me No resource found that matches the given name: attr 'custom:attribute'.
我已将 xmlns:custom="http://schemas.android.com/apk/res/com.my.package"
放在 style.xml 的所有标签中,包括、
和 ,但它仍然给我同样的错误,它可以找不到我的自定义 XML 命名空间.
I have put the xmlns:custom="http://schemas.android.com/apk/res/com.my.package"
in all of the tags in styles.xml, including <?xml>
, <resources>
, and <style>
, but it still gives me the same error, that it can't find my custom XML namespace.
但是,我可以使用我的命名空间在我的 layout.xml 中手动为视图分配属性,因此命名空间没有任何问题.我的问题在于让styles.xml 知道我的attr.xml.
I can, however, use my namespace to manually assign attributes to the view in my layout.xml, so there is nothing wrong with the namespace. My issue lies in making styles.xml aware of my attr.xml.
推荐答案
我想通了!答案是不要在样式中指定命名空间.
I figured it out! The answer is to NOT specify the namespace in the style.
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="custom_attr">value</item> <!-- tee hee -->
</style>
</resources>
这篇关于style.xml 中的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!