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

查看:39
本文介绍了自定义属性错误 - 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.

错误:多次找到项目属性/字体
错误:任务: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!

推荐答案

如您所见 here,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天全站免登陆