Android计算布局高度

来源:互联网 发布:金牌橱柜知乎 编辑:程序博客网 时间:2024/06/05 00:24

1、场景:父控件布局高度设置为wrap content,子控件设置为match parent.

1)父控件的onMeasure里边循环调用measureChild(childView, widthMeasureSpec, heightMeasureSpec). 然后调用childView.getMeasuredHeight(),并得出各子控件的高度。通过不同布局排列方式得出父控件的高度。

2)调用父控件的setMeasuredDimension设置父控件的高宽(该函数必须在onMeasure中被调用,以设置最终的尺寸,否则会抛出异常)

3)根据父控件高度生成MeasureSpec.EXACTLY的heightMeasureSpec,然后循环调用measureChildWithMargins(child, widthMeasureSpec, 0, uniformMeasureSpec, 0);





0 0