要想在View未在界面上显示之前就获得它的宽高,我们需要这样做
/**
* 目前该方法只支持预计算宽高设置为准确值或wrap_content的情况,
* 不支持match_parent的情况,因为view的父view还未预计算出宽高
* @param v 要预计算的view
*/
private void measureView(View v) {
ViewGroup.LayoutParams lp = v.getLayoutParams();
if (lp == null) {
return;
}
int width;
int height;
if (lp.width > 0) {
// xml文件中设置了该view的准确宽度值,例如android:layout_width="150dp"
width = View.MeasureSpec.makeMeasureSpec(lp.width, View.MeasureSpec.EXACTLY);
} else {
// xml文件中使用wrap_content设定该view宽度,例如android:layout_width="wrap_content"
width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
}
if (lp.height > 0) {
// xml文件中设置了该view的准确高度值,例如android:layout_height="50dp"
height = View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY);
} else {
// xml文件中使用wrap_content设定该view高度,例如android:layout_height="wrap_content"
height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
}
v.measure(width, height);
}
将要预算的view传入measureView方法,再调用getMeasuredWidth()、getMeasuredHeight()就可以获得将来实际显示的宽高了。