0

它是我的代码。referButtun 是 imageButton

@Override
protected void onResume() {
    super.onResume();
    referButtun.getHeight();
    Log.d("Base",""+referButtun.getHeight());
}
4

1 回答 1

1

这就是你应该这样做的方式:

final ViewTreeObserver vto = referButtun.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Log.d("Base",""+referButtun.getHeight());

        // don't forget to remove the listener, we don't need it just this one time
        vto.removeGlobalOnLayoutListener(this);
    }
});

作为替代方案,您可以使用

referButtun.post(new Runnable() {
    @Override
    public void run() {
        Log.d("Base",""+referButtun.getHeight());
    }
});
于 2013-06-19T16:50:05.257 回答