android onlayout参数
来源:互联网 发布:保时捷车载香水知乎 编辑:程序博客网 时间:2024/05/01 23:27
2. 重载onMeasure()方法
为什么要重载onMeasure()方法这里就不赘述了,上一篇文章已经讲过,这里需要注意的是,自定义ViewGroup的onMeasure()方法中,除了计算自身的尺寸外,还需要调用measureChildren()函数来计算子控件的尺寸。
onMeasure()的定义不是本文的讨论重点,因此这里我直接使用默认的onMeasure()定义,当然measureChildren()是必须得加的。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureChildren(widthMeasureSpec, heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
3. 实现onLayout()方法
onLayout()函数的原型如下:
//@param changed 该参数指出当前ViewGroup的尺寸或者位置是否发生了改变
//@param left top right bottom 当前ViewGroup相对于其父控件的坐标位置
protected void onLayout(boolean changed,int left, int top, int right, int bottom);
由于我们希望优先横向布局子控件,那么,首先,我们知道总宽度是多少,这个值可以通过getMeasuredWidth()来得到,当然子控件的宽度也可以通过子控件对象的getMeasuredWidth()来得到。
这样,就不复杂了,具体的实现代码如下所示:
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int mViewGroupWidth = getMeasuredWidth(); //当前ViewGroup的总宽度
int mPainterPosX = left; //当前绘图光标横坐标位置
int mPainterPosY = top; //当前绘图光标纵坐标位置
int childCount = getChildCount();
for ( int i = 0; i < childCount; i++ ) {
View childView = getChildAt(i);
int width = childView.getMeasuredWidth();
int height = childView.getMeasuredHeight();
//如果剩余的空间不够,则移到下一行开始位置
if( mPainterPosX + width > mViewGroupWidth ) {
mPainterPosX = left;
mPainterPosY += height;
}
//执行ChildView的绘制
childView.layout(mPainterPosX,mPainterPosY,mPainterPosX+width, mPainterPosY+height);
//记录当前已经绘制到的横坐标位置
mPainterPosX += width;
}
}
- android onlayout参数
- Android onLayout、layout区别
- Android onMeasure and onLayout
- Android中的OnMeasure及OnLayout
- Android API之onLayout, onMeasure
- Android中的OnMeasure及OnLayout
- 快速了解Android onMeasure() onLayout()
- Android自定义控件之onLayout
- Android自定义控件之onLayout
- Android自定义控件之onLayout
- Android onmeasure 与 onLayout示例
- android onLayout、layout方法学习
- Android 理解 onMeasure onLayout的过程
- Android自定义控件的onMeasure和onLayout
- android scrollview 滚动时会调用onlayout么
- Android的onLayout、layout方法讲解
- Android的onLayout、layout方法讲解
- android 中的onlayout onmeasure调用顺序
- 在JBOSS中配置JNDI
- 黑马程序员——java编程基础(三)数组
- Aizu 1311 Test Case Tweaking(spfa变形,dp)
- c++成员函数的重载、覆盖、隐藏区别
- Linux多线程一
- android onlayout参数
- Spring发送邮件总结(附源码)
- Self-define float4 vector
- 第2章2节《MonkeyRunner源码剖析》了解你的测试对象: NotePad窗口Activity之NotesList简介(原创)
- Android 点击EditText以外区域键盘消失
- 解决iframe跨域问题
- java 反射 根据属性 动态设置值
- 抽象工厂模式
- const 指针与指向const的指针