下面举个例子,在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 >