用自定义属性设置视图子类的自定义属性



如果我有以下设置:

public class ABCView extends View {
    //implementation here
}

具有以下自定义属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name = "ABCView" >
       <attr name="foo" format="boolean"/>
    </declare-styleable>
</resources>

如果我想用另一个视图子类化这个视图,但仍然能够在XML中指定自定义属性的值,我该怎么做?

public class DEFView extends ABCView {
    //implementation here
}

但是当我尝试在XML中使用子视图时,我得到了一个错误—它不认识到该属性的应用,因为它似乎不知道java类之间的关系。我该如何处理?

由于XML不支持java类层次结构,因此您可能还需要为子类视图显式指定自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name = "DEFView" >
       <attr name="foo" format="boolean"/>
    </declare-styleable>
</resources>

最新更新