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

相关推荐
2501_916007477 小时前
uni-app iOS 文件调试常见问题与解决方案:结合 itools、克魔、iMazing 的实战经验
android·ios·小程序·https·uni-app·iphone·webview
ljt27249606617 小时前
Compose笔记(四十九)--SwipeToDismiss
android·笔记·android jetpack
fatiaozhang95277 小时前
山西移动九联UNT413HS-海思MV320-2+8G-原机全量备份包
android·电脑·电视盒子·刷机固件·机顶盒刷机
某空m7 小时前
【Android】ViewPager2结合Fragment实现多页面滑动切换
android
旺小仔.8 小时前
Linux--命名管道
android·java·linux
热烈勒温10 小时前
Mybatis入门、操作数据、配置xml映射、数据封装
xml·mybatis
2501_9159184114 小时前
uni-app 项目 iOS 上架踩坑经验总结 从证书到审核的避坑指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者814 小时前
iOS 上架 uni-app 流程全解析,从打包到发布的完整实践
android·ios·小程序·https·uni-app·iphone·webview
雨白19 小时前
实现双向滑动的 ScalableImageView(上)
android
Y40900119 小时前
数据库基础知识——聚合函数、分组查询
android·数据库