安卓之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();//就获取到了子控件宽度
}
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();//就获取到了子控件宽度
}
相关文章推荐
- 10大开源Android项目
- UVa 1588 Kickdown
- select绑定json数组对象 asp.net
- HDU 5446 CRT+Lucas+快速乘
- 蓝懿iOS 内容和心得
- linux支持中文
- JavaScript-js中函数问题
- rtsp2rtmp程序,从rtsp摄像机获取流,转换为rtmp push到FMS
- input/select/textarea标签的readonly效果实现
- C# 如何执行bat文件 传参数
- Android Studio 1.4 新特性介绍
- codeforces #324 div 2 A. Olesya and Rodion
- 每天要更新了
- ElasticSearch的基本用法与集群搭建
- git学习笔记之一
- xcode-select: error: tool ‘xcodebuild(转)
- Linux下搭建Apache环境,执行Apache ab进行网站压力测试
- UITextField的leftView和rightView属性
- Part 97 Performance of a multithreaded program
- c# 程序后台运行