private void initView(){
//初始化布局
//假如想获取子布局高度
View header = LayoutInflater.from(this).inflate(R.layout.main,null);
measureView(header);
int headerHeight = header.getMeasuredHeight();
//其他操作
}
/**
* 获取宽高前,要通知父布局自己需要占用的大小
*/
private void measureView(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
int width = ViewGroup.getChildMeasureSpec(0, 0, p.width);
int height;
int tempHeight = p.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);
} else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECFIED);
}
view.measure(width, height);
}
//初始化布局
//假如想获取子布局高度
View header = LayoutInflater.from(this).inflate(R.layout.main,null);
measureView(header);
int headerHeight = header.getMeasuredHeight();
//其他操作
}
/**
* 获取宽高前,要通知父布局自己需要占用的大小
*/
private void measureView(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
int width = ViewGroup.getChildMeasureSpec(0, 0, p.width);
int height;
int tempHeight = p.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);
} else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECFIED);
}
view.measure(width, height);
}