简体   繁体   English

Android属性已经定义

[英]Android Attribute has already been defined

I am trying to make a custom view with an attribute named borderWidth . 我正在尝试使用名为borderWidth的属性创建自定义视图。 When i try to run my app i get the error: 当我尝试运行我的应用程序时,我收到错误:

E:\\Android Studio Projects\\MyApp\\app\\build\\intermediates\\res\\merged\\debug\\values\\values.xml Error:(341) Attribute "borderWidth" has already been defined E:\\ Android Studio Projects \\ MyApp \\ app \\ build \\ intermediates \\ res \\ merged \\ debug \\ values \\ values.xml错误:(341)属性“borderWidth”已定义

In the values.xml file that it points to, i can see that borderWidth is already being used for the default FloatingActionButton and a couple other default android widgets: 在它指向的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>

Can i reuse this name for my view or do i have to rename it? 我可以在我的视图中重复使用此名称,还是必须重命名?

Edit 编辑

This is the entire attrs.xml file: 这是整个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>

This is the layout file where my custom view is being used: 这是我使用自定义视图的布局文件:

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

You can reuse the attribute. 您可以重用该属性。 This is an example 这是一个例子

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

For more info https://developer.android.com/training/custom-views/create-view.html#customattr 有关详细信息, 请访问https://developer.android.com/training/custom-views/create-view.html#customattr

As the answer in this question states, the only way to use a particular name that has already been used by android is to re-use it. 正如这个问题中的答案所述,使用已经被android使用的特定名称的唯一方法是重用它。 This is not suitable in this situation however because my attributes have a different meaning from the standard android one, so i will just have to rename mine. 这不适合在这种情况下,因为我的属性与标准的android属性有不同的含义,所以我只需要重命名我的。

So i will be renaming borderWidth to graphBorderWidth . 所以我将borderWidth重命名为graphBorderWidth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 属性“ rippleColor”已经定义 - Attribute ”rippleColor“ has already been defined 属性“ raduis”已经定义 - Attribute “raduis” has already been defined 错误:(144)属性“srcCompat”已经定义 - Error:(144) Attribute “srcCompat” has already been defined 变量已经在我的方法中定义了,还是已经定义了? - Variable has already been defined in my method, or has it? 无法注册在 null 中定义的 bean 'postRepository'。 已在 null 中定义了具有该名称的 bean - The bean 'postRepository', defined in null, could not be registered. A bean with that name has already been defined in null 避免线程已启动异常(Android) - Avoid Thread has already been started exception (Android) 检查 BOOT_COMPLETED 是否已经在 Android 上被触发 - Check if BOOT_COMPLETED has been fired already on Android 从Vuforia Unity到Android-QCAR已初始化 - Vuforia Unity to Android - QCAR has already been initialized 如何检查FirebaseFirestore是否已在Android中启动? - How to check if FirebaseFirestore has already been started in Android? 如何调用已在Java android中的基本活动中定义的函数 - how to Calling function that has been defined in base activity in java android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM