2015-05-04 74 views
4

在我的Android項目中,我有幾個使用自定義屬性的自定義組件。自定義屬性錯誤 - Android Studio 1.2

的attrs.xml文件看起來是這樣的:

<?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> 

我拉在屬性只是在自定義組件很好,但是當我去跑我看到下面的錯誤代碼。

Error: Found item Attr/font more than one time
Error: Execution failed for task ':app:mergeDebugResources'.

它應該沒有區別,兩個不同的聲明樣式資源中有相似的屬性名稱是正確的嗎?

如果您有任何幫助,將不勝感激,謝謝!

+0

類似http://stackoverflow.com/questions/4434327/same-named-屬性在attrs xml爲自定義視圖 – Suragch

回答

3

如您所見,here,attr本身可以有多個屬性,並且只能定義一次,並且可以在其中配置多個詳細信息。 所以你應該給它不同的名稱,或者因爲它們具有相同的屬性,所以兩者只能使用一個declare-styable

退房this link也有一個很好的例子。

這是應該的:

<?xml version="1.0" encoding="utf-8"?> 
<resources > 
    <declare-styleable name="Box"> 
     <attr name="font" format="string"/> 
    </declare-styleable> 
</resources> 

您可以在文本,按鈕等使用Box

+2

哦,是的,我想我可以讓他們使用同一個,我沒有想到有聲明樣式除了它是關聯以外的任何名稱。這些鏈接非常有幫助,謝謝。 – MikaylaRay44