自定义属性错误 - Android Studio 1.2

时间:2021-01-16 20:56:40

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

在我的Android项目中,我有几个使用自定义属性的自定义组件。

The attrs.xml file looks like this:

attrs.xml文件如下所示:

<?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'.

错误:找到项目Attr / font多次错误:任务':app:mergeDebugResources'的执行失败。

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

在两个不同的declare-styleable资源中有类似的属性名称是否正确应该没有区别?

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

如果您有任何帮助,将不胜感激,谢谢!

1 个解决方案

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

正如您在此处所看到的,attr本身可以具有多个属性,并且只能定义一次,您可以在其中配置多个详细信息。所以你应该给它不同的名称,或者因为它们具有相同的属性,所以只使用一个declare-styable。

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.

你可以在文字,按钮等上使用Box。

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

正如您在此处所看到的,attr本身可以具有多个属性,并且只能定义一次,您可以在其中配置多个详细信息。所以你应该给它不同的名称,或者因为它们具有相同的属性,所以只使用一个declare-styable。

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.

你可以在文字,按钮等上使用Box。