自定义属性错误-Android Studio 1.2 [英] Custom Attribute Error - Android Studio 1.2

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

问题描述

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

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

attrs.xml文件如下所示:

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.

错误:发现项目Attr/font超过一次
错误:任务':app:mergeDebugResources'的执行失败.

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!

推荐答案

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

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.

这应该是这样:

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

您可以在文本,按钮等上使用Box.

You can use Box on text, button, etc.

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

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