因为哦点击按钮前的button的文字是获取验证码,点击之后的文字是59s->0s,所以这个button设置为wrap——content的时候导致宽度会变化,所以需要手动设置宽度不变,这里之前在button的带三个参数的构造函数里的获取的话为0,所以当时想到的此时视图还没全部渲染完成,因此,需要在其他地方获取,最后解决了,关键代码如下:
@Override
protected void onFinishInflate() {
super.onFinishInflate();
this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
VerifyButton.this.getViewTreeObserver().removeOnGlobalLayoutListener(this);
width = VerifyButton.this.getMeasuredWidth();
ToastUtils.showShort(""+width);
}
});
}