Android 属性已经定义
Android Attribute has already been defined
我正在尝试使用名为 borderWidth
的属性制作自定义视图。当我尝试 运行 我的应用时出现错误:
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>
您可以重复使用该属性。这是一个例子
<?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
正如 中的答案所述,使用已被 android 使用的特定名称的唯一方法是重新使用它。这在这种情况下不合适,但是因为我的属性与标准 android 的含义不同,所以我只需要重命名我的。
所以我会将 borderWidth
重命名为 graphBorderWidth
。
我正在尝试使用名为 borderWidth
的属性制作自定义视图。当我尝试 运行 我的应用时出现错误:
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>
您可以重复使用该属性。这是一个例子
<?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
正如
所以我会将 borderWidth
重命名为 graphBorderWidth
。