覆写onLayout进行layout,含自定义ViewGroup例子
来源:互联网 发布:淘宝尼克服女装品牌 编辑:程序博客网 时间:2024/05/21 19:26
视图的绘制仅在Framework层分为三个阶段measure,layout,draw。前一篇博文《 覆写onMeaure进行measure操作》 关于如何覆写onMeasure, 其目的是为了测量视图的大小也就是第一阶段,如果不了解或者关于onMeasure有什么疑惑可以了解下。 本篇博文是关于如何覆写onLayout,其目的是为了指定视图的显示位置,方法执行的前后顺序是在onMeasure之后,因为视图肯定是只有知道大小的情况下,才能确定怎么摆放。
一、自定义ViewGroup例子
例子代码 - 纵向显示两个TextView
(1)编写CustomViewGroup
- 1. CustomViewGroup继承自ViewGroup,ViewGroup是所有Layout的父类
- public class CustomViewGroup extends ViewGroup {
- }
2. 覆写View.onMeasure回调函数,用于计算所有child view的宽高,这里偷懒没有进行MeasureSpec模式判断
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- int widthSize = MeasureSpec.getSize(widthMeasureSpec);
- int heightSize = MeasureSpec.getSize(heightMeasureSpec);
- measureChildren(widthMeasureSpec, heightMeasureSpec);
- setMeasuredDimension(widthSize, heightSize);
- }
3. 覆写ViewGroup.onLayout回调函数,用于指定所有child View的位置
- @Override
- protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
- int mTotalHeight = 0;
- // 遍历所有子视图
- int childCount = getChildCount();
- for (int i = 0; i < childCount; i++) {
- View childView = getChildAt(i);
- // 获取在onMeasure中计算的视图尺寸
- int measureHeight = childView.getMeasuredHeight();
- int measuredWidth = childView.getMeasuredWidth();
- childView.layout(left, mTotalHeight, measuredWidth, mTotalHeight + measureHeight);
- mTotalHeight += measureHeight;
- Log.e(TAG, "changed = " + changed
- + ", left = " + left + ", top = " + top
- + ", right = " + right + ", bottom = " + bottom
- + ", measureWidth = " + measuredWidth + ", measureHieght = " + measureHeight);
- }
- }
3.1 onLayout回调函数参数:
left, top, right, bottom是当前ViewGroup整个在屏幕上的位置,手机屏幕是480 * 800 ,其中高度去除状态栏和title大概724
如果页面只有此ViewGroup则个参数的值
left = 0, top = 0, right = 480, bottom = 724
在此ViewGroup的上面放置一个TextView当期回调的参数值
left = 0, top = 25, right = 480, bottom = 724
3.2 child.layout如何填写参数
View是一个矩形,right和bottom也可以通过以下计算获得,其中Width 和 Height由onMeasure最终设置和决定
right = left + width
bottom = top + height
right = left + width
bottom = top + height
(2)创建Activity
- public class CustomViewGroupActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- CustomViewGroup customViewGroup = new CustomViewGroup(this);
- customViewGroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- // 添加子视图
- TextView textViewOne = new TextView(this);
- textViewOne.setText("a child view");
- textViewOne.setBackgroundColor(Color.BLUE);
- textViewOne.setLayoutParams(new LayoutParams(130, 130));
- customViewGroup.addView(textViewOne);
- TextView textViewTwo = new TextView(this);
- textViewTwo.setText("b child view");
- textViewTwo.setBackgroundColor(Color.RED);
- textViewTwo.setLayoutParams(new LayoutParams(80, 80));
- customViewGroup.addView(textViewTwo);
- setContentView(customViewGroup);
- }
- }
二 、扩展
× 三个方法的区别?后这个方法最终还是调用第一个方法
- childView.measure(widthMeasureSpec, heightMeasureSpec);
- measureChild(childView, widthMeasureSpec, heightMeasureSpec);
- measureChildren(widthMeasureSpec, heightMeasureSpec);
× 哪些视图属性影响 measure? padding、margin
× 哪些视图属性 影响 layout? gravity
× view的上下左右和measurespec width和height的关系 ?
三、资料
Android官方文档:
Layouts
http://developer.android.com/guide/topics/ui/declaring-layout.html
细说view流程上
http://blog.csdn.net/aaa2832/article/details/7844904
细说view流程下
http://blog.csdn.net/aaa2832/article/details/7849400
0 0
- 覆写onLayout进行layout,含自定义ViewGroup例子
- 覆写onLayout进行layout,含自定义ViewGroup例子
- 覆写onLayout进行layout,含自定义ViewGroup例子
- 覆写onLayout进行layout,含自定义ViewGroup例子
- 覆写onLayout进行layout,含自定义ViewGroup例子
- 覆写onLayout进行layout,含自定义ViewGroup例子
- 自定义view(四)覆写onLayout进行layout,含自定义ViewGroup
- ViewGroup的onLayout、layout方法
- 自定义ViewGroup onMeasure onLayout
- 自定义Layout用ViewGroup+ListView+GridView进行滑动事件拦截
- 【android】自定义ViewGroup的onLayout()分析
- 自定义view之layout()与onLayout()方法
- 自定义ViewGroup-自动换行Layout
- 自定义ViewGroup-自动换行Layout
- Android开发实践:自定义ViewGroup的onLayout()分析
- Android自定义ViewGroup:onMeasure与onLayout(1)
- Android开发实践:自定义ViewGroup的onLayout()分析
- 自定义viewgroup中onMeasure 和onlayout的一点理解
- Android中Surface和SurfaceView的一些理解和总结
- 关于网站中的优化百分比之重
- xcode制作越狱后ipa安装文件
- 常见浏览器兼容性问题与解决方案
- “玩转”Java系列—Struts2 运行流程分析
- 覆写onLayout进行layout,含自定义ViewGroup例子
- 计算机英语词汇
- oracle的substr函数的用法
- geoserver && openlayers
- Exchange 2013部署 Lync Server 2013企业版部署 Lync和Exchange 2013集成
- oracle中对排序的总结
- Android - 事件模型(dispatchTouchEvent , interceptTouchEvnet , onTouchEven)
- 计算机网络
- 设计模式Before-after之单例模式