码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-09 20:13:39      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:android   attr   xml   ui   

如果从单独开发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 >



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

标签:android   attr   xml   ui   

原文地址:http://blog.csdn.net/a2758963/article/details/39159365

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!