Android自定义View多个declare-styleable节点属性冲突处理

同一个Library中,不同的自定义View需要用不同的declare-styleable,如果两个不同的declare-styleable存在相同的属性就会构建失败,下面就是解决该问题的方法.

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <declare-styleable name="StepperIndicator" tools:ignore="ResourceName">
        <attr name="stpi_stepCount" format="integer"/>
    </declare-styleable>
    <declare-styleable name="StepperIndicator2" tools:ignore="ResourceName">
         <attr name="stpi_stepCount" format="integer"/>
    </declare-styleable>
</resources>

类似上面这样的写法就会导致构建失败,应该将相同的属性提取出来添加到resources节点中,然后在declare-styleable节点中引入该属性.

<resources xmlns:tools="http://schemas.android.com/tools">
    <attr name="stpi_stepCount" format="integer"/>
    <declare-styleable name="StepperIndicator" tools:ignore="ResourceName">
        <attr name="stpi_stepCount"/>
    </declare-styleable>
    <declare-styleable name="StepperIndicator2" tools:ignore="ResourceName">
         <attr name="stpi_stepCount"/>
    </declare-styleable>
</resources>

来源

猜你喜欢

转载自blog.csdn.net/MoLiao2046/article/details/108322205