多個類定義attr屬性重複的問題:Attribute "xxx" has already been defined

如果從單獨開發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 >



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章