2017-02-17 87 views
3

我正在嘗試使用名爲borderWidth的屬性進行自定義視圖。當我嘗試運行我的應用程序出現錯誤:Android屬性已被定義

E:\Android Studio Projects\MyApp\app\build\intermediates\res\merged\debug\values\values.xml Error:(341) Attribute "borderWidth" has already been defined

values.xml文件,它指向的,我可以看到borderWidth已被使用默認FloatingActionButton和其他幾個默認的Android小工具:

<style name="Widget.Design.FloatingActionButton" parent="android:Widget"> 
    <item name="android:background">@drawable/design_fab_background</item> 
    <item name="backgroundTint">?attr/colorAccent</item> 
    <item name="fabSize">auto</item> 
    <item name="elevation">@dimen/design_fab_elevation</item> 
    <item name="pressedTranslationZ">@dimen/design_fab_translation_z_pressed</item> 
    <item name="rippleColor">?attr/colorControlHighlight</item> 
    <item name="borderWidth">@dimen/design_fab_border_width</item> 
</style> 

我可以重複使用這個名稱作爲我的觀點,還是我必須重新命名?

編輯

這是整個attrs.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <!--<attr name="borderWidth" format="float" />--> 

    <declare-styleable name="ScrollingLineGraph"> 
     <attr name="unitsX" format="float" /> 
     <attr name="unitsY" format="float" /> 

     <attr name="scaleY" format="float" /> 
     <attr name="scaleX" format="float" /> 

     <!--<attr name="borderWidth" format="float" />--> 
     <attr name="scaleWidth" format="float" /> 

     <attr name="borderColor" format="color" /> 
     <attr name="scaleColor" format="color" /> 
     <attr name="lineColor" format="color" /> 
     <attr name="highlightColor" format="color" /> 
     <attr name="labelColor" format="color" /> 

     <attr name="labelSize" format="float" /> 
    </declare-styleable> 
</resources> 

這是我的自定義視圖正在使用的佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout android:id="@+id/coordinatorLayout_main" 
               xmlns:android="http://schemas.android.com/apk/res/android" 
               xmlns:custom="http://schemas.android.com/apk/res-auto" 
               android:layout_width="match_parent" 
               android:layout_height="match_parent" 
               android:fitsSystemWindows="false"> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="8dp" 
    android:orientation="vertical"> 

    <!-- Other views --> 

    <com.tycho.app.simplegraphs.ui.ScrollingLineGraph 
     android:id="@+id/graph1" 
     custom:unitsX="10000" 
     android:layout_width="200dp" 
     android:layout_height="40dp"/> 

    <com.tycho.app.simplegraphs.ui.ScrollingLineGraph 
     android:id="@+id/graph2" 
     android:layout_marginTop="4dp" 
     custom:lineColor="#FF0000" 
     custom:unitsX="10000" 
     custom:highlightColor="#80FF0000" 
     android:layout_width="200dp" 
     android:layout_height="40dp"/> 

    <!-- Other views --> 

</LinearLayout> 

回答

0

this question狀態答案,使用已經被使用由Android特定名稱的唯一方法是重新使用它。這在這種情況下不適合,因爲我的屬性與標準的android屬性有不同的含義,所以我只需要重命名我的。

所以我會重命名borderWidthgraphBorderWidth

0

你可以重用att ribute。這是一個例子

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="textSize" format="dimension"/> 

    <declare-styleable name="View1"> 
     <attr name="textSize"/> 
    </declare-styleable> 

    <declare-styleable name="View2"> 
     <attr name="textSize"/> 
    </declare-styleable> 
</resources> 

欲瞭解更多信息https://developer.android.com/training/custom-views/create-view.html#customattr

+0

我試過了,但它給出了以下錯誤:'錯誤:發現項目Attr/borderWidth多於一次' – TychoTheTaco

+0

您可以使用自定義命名空間。看看這裏https://developer.android.com/training/custom-views/create-view.html#customattr。您是否將自定義屬性添加到名爲attrs.xml的文件中? – Flood2d

+0

我已經有這樣的命名空間:'xmlns:custom =「http://schemas.android.com/apk/res-auto」'但錯誤仍然存​​在。 – TychoTheTaco