自定义View对layout、onLayout的一点心得

mac2025-10-02  1

https://blog.csdn.net/dmk877/article/details/49632959

 

https://blog.csdn.net/a396901990/article/details/38129669

 

推荐大家把第一个小例子做一下:

public class MyViewGrop extends ViewGroup { int scrrenWidth; public MyViewGrop(Context context) { this(context,null); } public MyViewGrop(Context context, AttributeSet attrs) { this(context, attrs,0); } public MyViewGrop(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); scrrenWidth = ((Activity)(context)).getWindowManager().getDefaultDisplay().getWidth(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { measureChildren(widthMeasureSpec,heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int size = getChildCount(); int mLeft = 10; int mTop = 10; for (int i=0;i<size;i++){ View childview = getChildAt(i); int childViewWidth = childview.getMeasuredWidth(); int childViewHight = childview.getMeasuredHeight(); childview.layout(mLeft,mTop,mLeft+childViewWidth,mTop+childViewHight); mLeft = mLeft + childViewWidth+10; if(mLeft+childViewWidth>scrrenWidth){ mLeft = 10; mTop = mTop + childViewHight + 10; } } } }

https://blog.csdn.net/gengkui9897/article/details/82810805

在了解View的layout和onLayout之前的问题解决:

1.layout()的作用是通过setFrame()方法,setFrame()方法调用mRenderNode.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom)方法设置本View的四个顶点大小,即确定View本身的位置,然后判断View是否改变了并返回是否改变(boolean值),如果改变了就需要调用onLayout,如果只是一个View不是ViewGrope则不需要重写onLayout(),这时layout()就能决定本View的位置。如果是ViewGrope则需要重写onLayout()去设置子View的位置(因:子View的确定位置与具体布局有关,所以onLayout()在ViewGroup没有实现)

2.onLayout()的作用是设置子View的位置,如果本View不是ViewGrope则是一个空实现,如果本View是一个ViewGrope

onLayout():

/** * 分析onLayout() * 作用:计算该ViewGroup包含所有的子View在父容器的位置() * 注: * a. 定义为抽象方法,需重写,因:子View的确定位置与具体布局有关,所以onLayout()在ViewGroup没有实现 * b. 在自定义ViewGroup时必须复写onLayout()!!!!! * c. 复写原理:遍历子View 、计算当前子View的四个位置值 & 确定自身子View的位置(调用子View layout()) */ protected void onLayout(boolean changed, int left, int top, int right, int bottom) { // 参数说明 // changed 当前View的大小和位置改变了 // left 左部位置 // top 顶部位置 // right 右部位置 // bottom 底部位置 // 1. 遍历子View:循环所有子View for (int i=0; i<getChildCount(); i++) { View child = getChildAt(i); // 2. 计算当前子View的四个位置值 // 2.1 位置的计算逻辑 ...// 需自己实现,也是自定义View的关键 // 2.2 对计算后的位置值进行赋值 int mLeft = Left int mTop = Top int mRight = Right int mBottom = Bottom // 3. 根据上述4个位置的计算值,设置子View的4个顶点:调用子view的layout() & 传递计算过的参数 // 即确定了子View在父容器的位置 child.layout(mLeft, mTop, mRight, mBottom); // 该过程类似于单一View的layout过程中的layout()和onLayout(),此处不作过多描述 } } }

 

 

2.布局从头到尾的一个逻辑:从ViewRootImpl的performTraversals的performLayout方法开头

首先明确:

(1).调用关系是父View通过调用子View的layout方法进行逻辑传递,layout方法会首先设置自己的布局然后再调用onLayout方法去设置子View的布局

(2).layout方法所有的ViewGrope和View都是用的View的layout方法

(3).onLayout方法只有ViewGrope重写

 

逻辑:

       最开始是调用decorView的layout方法,然后decorView先设置自己的布局然后再走onLayout方法去调用子View的layout方法设置子View的布局,然后就递归向下直到最底层结束。

 

 

 

 

 

 

最新回复(0)