设为首页 加入收藏

TOP

Android之自定义ViewGroup和自动换行的布局的实现
2014-11-24 13:22:19 来源: 作者: 【 】 浏览:1
Tags:Android 定义 ViewGroup 自动 布局 实现

  这里有个地方要注意,那就要明白ViewGroup的绘图流程:ViewGroup绘制包括两个步骤:1.measure 2.layout


  在两个步骤中分别调用回调函数:1.onMeasure() 2.onLayout()


  1.onMeasure() 在这个函数中,ViewGroup会接受childView的请求的大小,然后通过childView的 measure(newWidthMeasureSpec, heightMeasureSpec)函数存储到childView中,以便childView的getMeasuredWidth() andgetMeasuredHeight() 的值可以被后续工作得到。


  2.onLayout() 在这个函数中,ViewGroup会拿到childView的getMeasuredWidth() andgetMeasuredHeight(),用来布局所有的childView。


  3.View.MeasureSpec 与 LayoutParams 这两个类,是ViewGroup与childView协商大小用的。其中,View.MeasureSpec是ViewGroup用来部署 childView用的, LayoutParams是childView告诉ViewGroup 我需要多大的地方。


  4.在View 的onMeasure的最后要调用setMeasuredDimension()这个方法存储View的大小,这个方法决定了当前View的大小。


  


  效果图:



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android的基站、WIFI、GPS定位集.. 下一篇JAVA IO操作总结——节点流和处理..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: