通过如下方法获取宽高:
int width = v.getMeasuredWidth();
int height = v.getMeasuredHeight();
一、重写Activity的onWindowFocusChanged方法(当view布局完成后会触发此方法):
public void onWindowFocusChanged(boolean hasFocus){super.onWindowFocusChanged(hasFocus);
int width = v.getMeasuredWidth();
int height = v.getMeasuredHeight();
}
二、使用ViewTreeObserver(当view发生改变时触发):
View v = ...;v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout(){
int vWidth = v.getMeasuredWidth();
int vHeight = v.getMeasuredHeight();
v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});