Java 在扩展类中的重写方法上访问自定义xml值

Java 在扩展类中的重写方法上访问自定义xml值,java,android,xml,Java,Android,Xml,我扩展了Button类,在这个类中,我重写了这个方法 设置复合可绘图项将可绘图项缩小到按钮大小。我为XML元素设置了一个自定义值,我需要访问它来计算我将给可绘制元素的大小。我使用typedarray执行典型的操作来检索值,就在构造函数中的超级调用之后,但是当应用程序调用setCompoundDrawables时,应用程序还没有读取自定义XML值,因此我总是为自定义变量获取相同的(默认)值 一些代码: public MyButton(Context context, AttributeSet at

我扩展了Button类,在这个类中,我重写了这个方法 设置复合可绘图项将可绘图项缩小到按钮大小。我为XML元素设置了一个自定义值,我需要访问它来计算我将给可绘制元素的大小。我使用typedarray执行典型的操作来检索值,就在构造函数中的超级调用之后,但是当应用程序调用setCompoundDrawables时,应用程序还没有读取自定义XML值,因此我总是为自定义变量获取相同的(默认)值

一些代码:

public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    init(attrs);
}


public void init(AttributeSet attrs) {
    myValue = true;
    if (attrs != null) {
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.MyButton,
                0, 0);
        try {
            myValue = a.getBoolean(R.styleable.MyButton_myValue, true);
        } finally {
            a.recycle();
        }
    }
    Log.d(TAG, "myValue (init) = "+myValue);
}

@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
    Log.d(TAG, "myValue (setComp...) = "+myValue);
    ...
    super.setCompoundDrawables(left, top, right, bottom);
}
当我运行我的应用程序时,我会按以下顺序打印:

myValue (setComp...) = false
myValue (init) = true
有人能帮我吗


谢谢

我通过这样做来解决这个问题:Drawable[]d=getCompoundDrawables();setCompoundDrawables(d[0],d[1],d[2],d[3]);在init方法的末尾,它是有效的,但我认为这不是解决这个问题的方法…我通过这样做来解决这个问题:Drawable[]d=getCompoundDrawables();setCompoundDrawables(d[0],d[1],d[2],d[3]);在init方法的末尾,它是有效的,但我认为这不是实现这一点的方法。。。