您的位置:首页 > 其它

安卓之onmeasure方法 获取子控件

2015-10-11 23:55 357 查看
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){

int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);  //  获取当前父容器(Flowlayout)的模式

width = MeasureSpec.getSize(widthMeasureSpec)-getPaddingLeft()-getPaddingRight();
int height = MeasureSpec.getSize(heightMeasureSpec)-getPaddingBottom()-getPaddingTop(); // 获取到宽和高 

childeWidthMode=widthMode==MeasureSpec.EXACTLY?MeasureSpec.AT_MOST:widthMode;

childeHeightMode=heightMode==MeasureSpec.EXACTLY?MeasureSpec.AT_MOST:heightMode;//判定规则如图一样

//获取了子控件的测量规则

int childWidthMeasureSpec=MeasureSpec.makeMeasureSpec(childeWidthMode,  width);
int childHeightMeasureSpec=MeasureSpec.makeMeasureSpec(childeHeightMode,  height);

//根据自己方法获取子控件总数

View child=getChildAt(i);

child.measure(childWidthMeasureSpec, childHeightMeasureSpec);//进行测量

int measuredWidth = child.getMeasuredWidth();//就获取到了子控件宽度

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: