我的代码。 referButtun是imageButton
@Override
protected void onResume() {
super.onResume();
referButtun.getHeight();
Log.d("Base",""+referButtun.getHeight());
}
答案 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());
}
});