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的问题,相对而言,更推荐方式二的解决办法。方式二可以提高同名属性的重复性和利用率。

相关推荐
alexhilton44 分钟前
深入浅出着色器:极坐标系与炫酷环形进度条
android·kotlin·android jetpack
一条上岸小咸鱼7 小时前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
Huntto7 小时前
最小二乘法计算触摸事件速度
android·最小二乘法·触摸事件·速度估计
一笑的小酒馆7 小时前
Android中使用Compose实现各种样式Dialog
android
红橙Darren7 小时前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.11 小时前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架
来来走走16 小时前
Flutter开发 网络请求
android·flutter
独行soc1 天前
2025年渗透测试面试题总结-18(题目+回答)
android·python·科技·面试·职场和发展·渗透测试
雨白1 天前
登录和授权:Cookie与Authorization Header机制详解
android
Frank_HarmonyOS1 天前
【Android -- 多线程】Handler 消息机制
android