多个类定义attr属性重复的问题:Attribute "xxx" has already been defined

时间:2021-01-09 20:33:09
如果从单独开发app的话,可能不会遇到多个自定义类的attribute 的名字重复的问题。但是如果是团队合作开发的话,可能会碰到这样的问题,A和B自定义的两个类都用了同一个名字来定义属性,这时系统会报出警告,Attribute "xxx" has already been defined. A和B又都不想修改自己的名字,这时就很头痛。
下面举个例子,在values文件夹下定义一个上面的attrs.xml的文件,eclipse即会报错:Attribute "icon" has already been defined。因为在PreferenceHeader, Preference两个属性集里定义了两个相同的属性。
<? xml version = "1.0" encoding = "utf-8" ?>
< resources >
< declare-styleable name= "PreferenceHeader" >
<!-- Identifier value for the header. -->
< attr name= "id" format = "integer"/>
< attr name= "icon" format = "integer" />
<!-- The fragment that is displayed when the user selects this item. -->
</declare-styleable >
< declare-styleable name= "Preference" >
< attr name= "icon" format = "integer" />
<!-- The key to store the Preference value. -->
< attr name= "key" format = "string" />
</declare-styleable >
</ resources >

解决方案:1.在xml文件里前面先声明属性2.然后在属性集合里引用声明的属性即可。以上面的文件为例来修改,如下:
<? xml version = "1.0" encoding = "utf-8" ?>
< resources >
< attr name = "icon" format = "integer" />
< declare-styleable name= "PreferenceHeader" >
<!-- Identifier value for the header. -->
< attr name= "id" format = "integer"/>
< attr name= "icon" />
<!-- The fragment that is displayed when the user selects this item. -->
</declare-styleable >
< declare-styleable name= "Preference" >
< attr name= "icon" />
<!-- The key to store the Preference value. -->
< attr name= "key" format = "string" />
</declare-styleable >
</ resources >