Error:error: resource previously defined here. AAPT2 error: check logs for details

如果你遇到了以下的问题:

这里写图片描述

说明你可能遇到跟我一样的问题了。

AAPT

AAPT是Android Asset Packaging Tool的缩写,即安卓文件打包工具。作用是将安卓资源文件编译成二进制文件。

问题产生

我在自定义View的时候,自定义了attr属性,添加了一个textColor的属性值,为的是接收文字颜色值。

<declare-styleable name="RollTextView">
        <attr name="textColor" format="color"/>
        <attr name="textSize" />
</declare-styleable>

结果运行的时候就出现了上述的问题,原因是因为重定义了系统的属性值。

解决办法

1、重命名textColor,名称不与系统的textColor一样即可。

<declare-styleable name="RollTextView">
        <attr name="text_color" format="color"/>
        <attr name="textSize" />
</declare-styleable>

2、不对textColor进行重命名,直接引用系统的textColor,然后在xml里面使用时,就不能使用自定义的命名空间了(例如:app:),得用使用原生的引用(android:),千里之堤,毁于蚁穴,稍加注意就好。

<declare-styleable name="RollTextView">
        <attr name="textColor" />
        <attr name="textSize" />
</declare-styleable>

特此做个小笔记。

发布了27 篇原创文章 · 获赞 32 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/luoyingxing/article/details/79156501