在styles.xml自定义属性 [英] Custom attributes in styles.xml

查看:145
本文介绍了在styles.xml自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义部件,而我宣布它layout.xml。我还添加了attr.xml一些自定义属性。然而,试图在styles.xml样式声明这些属性时,它给了我未发现的资源匹配给定名称:ATTR。自定义:属性

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:定制=htt​​p://schemas.android.com/apk/res/com.my.package中的所有标签在styles.xml,包括< 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="customAttr">value</item> <!-- tee hee -->
    </style>
</resources>

这篇关于在styles.xml自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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