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

相关推荐
阿pin1 小时前
Android随笔-Zygote中fork究竟是什么?
android·zygote·fork
Go-higher1 小时前
DriverTest 驾考知识卡片学习助手 —— 一款基于 Jetpack Compose 的现代 Android 学习APP
android·学习
安卓修改大师1 小时前
安卓修改大师APK控件修改实战教程
android
阿pin2 小时前
Android随笔-Zygote是什么?
android·zygote
小虎牙0072 小时前
Android kotlin图片库Coil源码详解
android·前端
AFinalStone2 小时前
Android 7系统网络(一)全景图与调用链路概览
android·网络·frameworks
用户86022504674723 小时前
Android DEX 内存 Dump 全流程实战:从 APK 提取到无特征内存盲扫
android
Android-Flutter6 小时前
android compose Brush 渐变和着色器 使用
android·kotlin·compose
杉氧6 小时前
兼容与共生:如何在旧项目中优雅地引入 Compose?
android·架构·android jetpack
Flynt7 小时前
Room 3.0 包名重构 + KMP 迁移:我把项目升级踩了个遍
android·数据库·kotlin