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

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值