Android 在attrs.xml添加属性时出现 Found item Attr/****** more than one time

Android 在attrs.xml添加属性时出现 Found item Attr/****** more than one time

问题描述

在Android应用开发过程中,经常需要自定义控件,并且定义控件的属性,方便灵活的修改控件的显示样式,提高代码的可重用性和拓展性。但是在attrs.xml文件定义控件的属性,编译工程时报错了。

Found item Attr/axis_x_min more than one time

attrs.xml 文件中有多个同名条目时,通常会出现Found item Attr/****** more than one time错误消息。 attrs.xml 中的每个属性名称必须是唯一的。其实就是你的attrs.xml中有同名的属性(比如line_number)。

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="ChartView1">
        <attr name="axis_left_min" format="float"/>
        <attr name="axis_left_max" format="float"/>
        <attr name="axis_right_min" format="float"/>
        <attr name="axis_right_max" format="float"/>
        <attr name="line_number" format="integer"/>
    </declare-styleable>

    <declare-styleable name="ChartView2">
        <attr name="axis_x_min" format="float"/>
        <attr name="axis_x_max" format="float"/>
        <attr name="axis_y_min" format="float"/>
        <attr name="axis_y_max" format="float"/>
        <attr name="line_number" format="integer"/>
    </declare-styleable>

</resources>

解决办法

方式一

修改同名的属性,使不同控件的属性名不一样。将ChartView1控件的line_number属性改名为line_number_chart1ChartView2控件的line_number属性改名为line_number_chart2,这样就保证了属性名的唯一。

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="ChartView1">
        <attr name="axis_left_min" format="float"/>
        <attr name="axis_left_max" format="float"/>
        <attr name="axis_right_min" format="float"/>
        <attr name="axis_right_max" format="float"/>
        <attr name="line_number_chart1" format="integer"/>
    </declare-styleable>

    <declare-styleable name="ChartView2">
        <attr name="axis_x_min" format="float"/>
        <attr name="axis_x_max" format="float"/>
        <attr name="axis_y_min" format="float"/>
        <attr name="axis_y_max" format="float"/>
        <attr name="line_number_chart_2" format="integer"/>
    </declare-styleable>

</resources>

方式二

将相同的属性定义到declare-styleable标签外面,内部只声明引用,这样不同控件就可以重复使用了。

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="line_number" format="integer"/>

    <declare-styleable name="ChartView1">
        <attr name="axis_left_min" format="float"/>
        <attr name="axis_left_max" format="float"/>
        <attr name="axis_right_min" format="float"/>
        <attr name="axis_right_max" format="float"/>
        <attr name="line_number" />
    </declare-styleable>

    <declare-styleable name="ChartView2">
        <attr name="axis_x_min" format="float"/>
        <attr name="axis_x_max" format="float"/>
        <attr name="axis_y_min" format="float"/>
        <attr name="axis_y_max" format="float"/>
        <attr name="line_number" />
    </declare-styleable>

</resources>

小结

通过上述两种方式,我们都可以解决Found item Attr/****** more than one time的问题,相对而言,更推荐方式二的解决办法。方式二可以提高同名属性的重复性和利用率。

相关推荐
阿pin2 小时前
Android随笔-kotlin withContext
android·kotlin·withcontext
树码小子3 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea
Android-Flutter4 小时前
android 动画详解
android·kotlin
小孔龙9 小时前
GraphicBuffer 跨进程共享
android
行业研究员10 小时前
Android内置GPS与腾讯地图定位技术对比分析
android·android定位·腾讯地图定位
Android-Flutter10 小时前
android WMS 详解(二)
android·kotlin
游戏开发爱好者811 小时前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
游戏开发爱好者812 小时前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
阿pin13 小时前
Android随笔-kotlin 高阶函数
android·kotlin·高阶函数
wxson72821 天前
【Android视频监控系统技术总结】
android·音视频