android - 使用自定义属性设置View子类的自定义属性?

标签 android android-layout

如果我有以下设置:

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>

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

public class DEFView extends ABCView {
    //implementation here
}

但是当我尝试在 XML 中使用 subview 时,出现错误 - 它无法识别该属性适用,因为它似乎不知道 java 类之间的关系。我该如何处理这个问题?

最佳答案

由于 XML 不涉及 java 类层次结构,因此您可能还需要为子类 View 显式指定自定义属性。

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

关于android - 使用自定义属性设置View子类的自定义属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432178/

相关文章:

android - html5 音频元素用 jquery 返回 src

java - eclipse ADT 速度减慢和错误

android - iOS 上的蓝牙连接

android - 如何在7英寸两个不同的平板电脑上设置布局和绘图?

Android:ScaleType ="fitXY"不适用于所有图像

android - 从深层链接中排除少数网址

Android ActionBar - 如何检查当前可见的操作栏按钮数量?

java - 用图像网格填充屏幕的最佳方法

布局复杂的Android ui

java - 访问和编辑自定义适配器的 subview