为什么view.getHeigth在onResume()上返回0?

时间:2013-06-19 16:10:00

标签: android android-button

我的代码。 referButtun是imageButton

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

1 个答案:

答案 0 :(得分: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());
    }
});