Custom Attribute Error - Android Studio 1.2

最后都变了- 提交于 2019-12-07 02:39:38

问题


In my Android project I have a couple of custom components that use custom attributes.

The attrs.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources >
    <declare-styleable name = "TextBox">
        <attr name = "font" format = "string"/>
    </declare-styleable>

    <declare-styleable name = "ButtonBox">
        <attr name = "font" format = "string"/>
    </declare-styleable>
</resources>

I am pulling in the attributes just fine in the custom component, but when I go to run the code I see the following error.

Error: Found item Attr/font more than one time
Error: Execution failed for task ':app:mergeDebugResources'.

It shouldn't make a difference that there are similar attribute names in two different declare-styleable resources correct?

If you have any help it would be greatly appreciated, thank you!


回答1:


As you can see here, the attr itself can have multiple properties and can be defined only once and you can configure multiple details inside it. So you should give it different names or, since they have the same properties, use only one declare-styable for both.

Check out this link too, there's a good example.

Here is how it should be:

<?xml version="1.0" encoding="utf-8"?>
<resources >
    <declare-styleable name="Box">
        <attr name="font" format="string"/>
    </declare-styleable>
</resources>

You can use Box on text, button, etc.



来源:https://stackoverflow.com/questions/30038982/custom-attribute-error-android-studio-1-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!