美文网首页
2.布局过程之布局阶段

2.布局过程之布局阶段

作者: 真胖大海 | 来源:发表于2018-04-29 19:56 被阅读77次

1. 什么是布局阶段

ViewGroup摆放子View的位置

2.布局阶段原理

  1. 在父容器的onlayout()中根据在测量阶段测量出的子View的尺寸和自己的布局规则,计算出子View的绘制位置(left,top,right,bottom)
  2. 调用子View的layout(l,t,r,b)方法,View会记录下绘制的位置(应该是留给绘制的时候使用)。
  3. 在子View的layout方中如果会调用子View的onLayout 方法,如果子View是容器则onLayout中会重复1,2步,如果不是则onLayout没有任何实现

3.源码分析

父容器以AbsoluteLayout为例

public class AbsoluteLayout{
    @Override
    protected void onLayout(boolean changed, int l, int t,
            int r, int b) {
        int count = getChildCount();

        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {

                AbsoluteLayout.LayoutParams lp =
                        (AbsoluteLayout.LayoutParams) child.getLayoutParams();

                int childLeft = mPaddingLeft + lp.x;
                int childTop = mPaddingTop + lp.y;
                child.layout(childLeft, childTop,
                        childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());

            }
        }
    }
    }

View

pubic

public void layout(int l, int t, int r, int b) {

 boolean changed =
public void layout(int l, int t, int r, int b) {
isLayoutModeOptical(mParent) ?
               setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
               
onLayout(changed, l, t, r, b);

}


protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
}

4.注意

  1. measure是调度方法,onMeasure确定视图的大小。
  2. layout确定视图的位置,onLayout确定子视图的位置。
  3. View.java和ViewGroup.java都没有实现onLayout
    1. View.java没有实现onLayout 是因为View.java没有子View
    2. ViewGroup.java没有实现onLayout则所用,所有继承ViewGroup的容器需要根据自己的规则计算子View的位置

5.demo

demo地址

相关文章

  • 2.布局过程之布局阶段

    1. 什么是布局阶段 ViewGroup摆放子View的位置 2.布局阶段原理 在父容器的onlayout()中根...

  • 布局阶段

    这一阶段是对于布局的设置,因为对于一般的软件而言,当你点击进去某个activity时,往往这个activity与之...

  • 1.布局过程之测量阶段

    1.什么是布局过程 布局是计算控件大小和位置的过程。 布局过程分为两个阶段 测量阶段计算控件及其子控件的大小 布局...

  • 建立app的底部导航栏

    一、Android studio创建好工程之后首先主布局:采用线性布局//用来装碎片的

  • CSS布局

    1. 单栏布局 inline-block Flex布局 2. 两栏布局 Float布局 Flex布局 3. 三栏布...

  • 第十七章 Android中的布局

    一、Android的布局类型 1.线性布局(LinearLayout)2.相对布局(RelativeLayout)...

  • 2018-05-22

    关于网页设计的布局 常用的设计布局分为: •1. “国”字型布局 •2. “T”字型布局 •3. 标题正文型布局 ...

  • android布局

    1.线性布局LinearLayout 2.相对布局RelativeLayout 3.绝对布局FrameLayout...

  • Android的五大布局

    Android有五大布局: 1.RelativeLayout 相对布局 2. LinearLayout 线性布局 ...

  • iOS 中利用相对布局和绝对布局,对Table中的文字自适应调整

    demo地址: dealText 相对布局 与 绝对布局 1.相对布局: 2.绝对布局: 本demo中自适应高度的...

网友评论

      本文标题:2.布局过程之布局阶段

      本文链接:https://www.haomeiwen.com/subject/lilalftx.html