Android开发——Error: Found item Attr/tabTextSize more than one time解决方法

起因

我在res/values/atts.xml文件中定义了tabTextSize属性。使用的时候是没有问题,但是我在编译的过程就产生了Error: Found item Attr/tabTextSize more than one time,这句话的意思是错误:发现项目attr/TabTrimeScrize定义不止一次,开始的时候百思不得其解,于是我去看了attr里面定义的属性。发现下面的显示的那样:

<resources>
    <declare-styleable name="View名称">
        <attr name="tabTextSize" format="float"/>
        <attr name="tabTextSelectedColor" format="color" />
        <attr name="tabTextUnselectedColor" format="color" />
     </declare-styleable>

    <declare-styleable name="View名称">
        <attr name="tabTextSize" format="float"/>
        <attr name="tabTextSelectedColor" format="color" />
        <attr name="tabTextUnselectedColor" format="color" />
    </declare-styleable>
</resources>

由于自定义View属性名称相同所以产生了上面开始提到的错误。

解决方法

解决方法,我把其中相同的属性抽出来定义就可以。如下面所示将<attr name="tabTextSize" format="float" />抽来,然后在每个View里面调用就可以了。

<resources>
    <attr name="tabTextSize" format="float"/>
    <declare-styleable name="View名称">
        <attr name="tabTextSize" />
        <attr name="tabTextSelectedColor" format="color" />
        <attr name="tabTextUnselectedColor" format="color" />
    </declare-styleable>

    <declare-styleable name="View名称">
        <attr name="tabTextSize"/>
        <attr name="tabTextSelectedColor" format="color" />
        <attr name="tabTextUnselectedColor" format="color" />
    </declare-styleable>
</resources>
这个问题通常是由于 Android Support 库版本不兼容导致的。解决方法如下: 1. 在 app 模块的 build.gradle 文件中,将 targetSdkVersion 与 compileSdkVersion 版本升级到最新的 Android 版本。 ``` android { compileSdkVersion 31 targetSdkVersion 31 ... } ``` 2. 确保在 dependencies 中使用最新的 Support 库版本。可以在项目根目录下的 build.gradle 文件中设置全局的 Support 库版本: ``` ext { supportLibVersion = "28.0.0" } subprojects { ... dependencies { ... implementation "com.android.support:appcompat-v7:$supportLibVersion" implementation "com.android.support:design:$supportLibVersion" ... } } ``` 然后在 app 模块的 build.gradle 文件中使用该全局变量: ``` dependencies { ... implementation "com.android.support:appcompat-v7:$supportLibVersion" implementation "com.android.support:design:$supportLibVersion" ... } ``` 3. 如果还是出现上述错误,可以尝试使用 AndroidX 库替换 Support 库。在 app 模块的 build.gradle 文件中添加以下代码: ``` android { ... defaultConfig { ... // 添加以下两行 useLibrary 'androidx.core' javaCompileOptions { annotationProcessorOptions { includeCompileClasspath false } } } ... } ``` 然后将 Support 库依赖替换为对应的 AndroidX 库依赖即可。例如: ``` dependencies { ... implementation 'androidx.appcompat:appcompat:1.4.0' implementation 'com.google.android.material:material:1.5.0-alpha01' ... } ``` 以上是解决 AAPT 错误的一些方法,希望能够帮助到你。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值