/** * ListView中提前测量View尺寸,如headerView */ 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); }
//第一个参数spec =0,mode是UNSPECIFIED(未指定),父元素不对子元素施加任何束缚,第二个参数,是外边距和内边距 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.UNSPECIFIED); } view.measure(width, height); }