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

相关推荐
Lei活在当下10 分钟前
【业务场景架构实战】8. 订单状态流转在 UI 端的呈现设计
android·设计模式·架构
小趴菜822723 分钟前
Android中加载unity aar包实现方案
android·unity·游戏引擎
qq_2529241927 分钟前
PHP 8.0+ 现代Web开发实战指南 引
android·前端·php
Jeled27 分钟前
Android 本地存储方案深度解析:SharedPreferences、DataStore、MMKV 全面对比
android·前端·缓存·kotlin·android studio·android jetpack
2501_915918417 小时前
掌握 iOS 26 App 运行状况,多工具协作下的监控策略
android·ios·小程序·https·uni-app·iphone·webview
2501_9159090610 小时前
iOS 混淆实战,多工具组合完成 IPA 混淆与加固(源码 + 成品 + 运维一体化方案)
android·运维·ios·小程序·uni-app·iphone·webview
*才华有限公司*11 小时前
安卓前后端连接教程
android
氦客11 小时前
Android Compose中的附带效应
android·compose·effect·jetpack·composable·附带效应·side effect
雨白12 小时前
Kotlin 协程的灵魂:结构化并发详解
android·kotlin