自定义view onLayout
来源:互联网 发布:java 网店系统 手机 编辑:程序博客网 时间:2024/05/16 09:50
自定义view onLayout
measure过程结束后,视图测量完毕,就需要进行layout过程。layout过程源码如下
public void layout(int l, int t, int r, int b) { int oldL = mLeft; int oldT = mTop; int oldB = mBottom; int oldR = mRight; boolean changed = setFrame(l, t, r, b); if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) { if (ViewDebug.TRACE_HIERARCHY) { ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT); } onLayout(changed, l, t, r, b); mPrivateFlags &= ~LAYOUT_REQUIRED; if (mOnLayoutChangeListeners != null) { ArrayList<OnLayoutChangeListener> listenersCopy = (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone(); int numListeners = listenersCopy.size(); for (int i = 0; i < numListeners; ++i) { listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB); } } } mPrivateFlags &= ~FORCE_LAYOUT; }
在上面的layout方法中,首先通过传入的l, t, r, b进行判断是否需要重绘,我们也知道如果view的布局并没有发生变化,就不需要进行再一次的重新计算布局。在上面的代码中还可以看到调用了onLayout方法,这也就是我们经常需要重写的方法。在onLayout方法中我们可以根据自己的需要进行对子view的布局操作。
而如果是view的onLayout则经常直接调用super方法,不需要对其进行操作。
@Override protected abstract void onLayout(boolean changed,int l, int t, int r, int b);
传入l t r b分别代表相对于父布局的左边距,上边距,右边距和下边距。
当我们重写viewGroup时候,需要对onLayout进行重写,从而判定得到子view的摆放方式,通过调用子view的layout方法,传入计算好的 l, t,r,b即可。
“`
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 获得子View个数
int childCount = getChildCount(); // 父View左侧的距离 int mLeft = 0; // 遍历子View for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); // 获得子View的高度 int childViewHeight = childView.getMeasuredHeight(); // 获得子View的宽度 int childViewWidth = childView.getMeasuredWidth(); // 让子View在竖直方向上显示在屏幕的中间位置 int height = sreenH / 2 - childViewHeight / 2; // 调用layout给每一个子View设定位置mLeft,mTop,mRight,mBottom.左上右下 childView.layout(mLeft, height, mLeft + childViewWidth, height + childViewHeight); // 改变下一个子View到父View左侧的距离 mLeft += childViewWidth; }}
一个简单的demo实现简单的横向排列效果
阅读全文
0 0
- 自定义view中的onLayout
- 自定义view onLayout
- 自定义View的onMeasure、onLayout
- 自定义View-3-重写onLayout
- 自定义View初探-onLayout详解
- 自定义View(三) 之 onMeasure() onLayout() onDraw()
- 自定义View之onMeasure(),onLayout(),onDraw()
- 自定义View中的onMeasure,onLayout,onDraw
- 自定义View中的onMeasure,onLayout,onDraw
- 【自定义View系列】03--onLayout源码分析
- 自定义View之onLayout方法学习
- Android自定义View基础之onLayout详解
- Android 自定义View总结 —— onLayout()
- Android 自定义 View 之 onLayout 源码分析
- Android 自定义View基础 onMeasure & onLayout
- 自定义view之onMeasure,onLayout,onDraw
- 自定义view之layout()与onLayout()方法
- Android自定义View-onLayout、onSizeChange、onDraw介绍
- 环形链表约瑟夫问题(可参考剑指offer面试题62)
- 一篇文章解读提速、降费黑科技:PCDN定义、功能、架构、场景和优势
- Logstash学习总结(二) INPUT应用实例1
- 解决Servlet存在却报ClassNotFound问题——jar包放入WEB/INF/lib 与 userLibrary引入的区别
- iOS 多次请求只执行一次
- 自定义view onLayout
- SSD-Tensorflow学习
- uSens凌感推出基于SLAM的移动端Inside-out位置追踪技术
- 大牛给计算机专业学生的7个建议
- Python起步之面向对象程序设计
- MMU和cache详解(TLB机制)
- 运算符重载(C++)2
- 2017(二)软件安装
- BZOJ 3732 Network —— 最小生成树 + 倍增LCA