android View的布局过程

Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in measure(int, int) and is a top-down traversal of the view tree. Each view pushes dimension specifications down the tree during the recursion. At the end of the measure pass, every view has stored its measurements. The second pass happens in layout(int, int, int, int) and is also top-down. During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.

(View)的布局分两步进行:测量过程和布局过程。测量过程就是使用measure方法和从上往下遍历view树进行的。每一个view通过递归的方式在view树上从上往下确定自己的尺寸参数(这句翻译感觉欠妥),在测量过程结束时,每一个view都存储了他自己的尺寸值。第二个过程是伴随layout()方法开始的,也是一个从上往下的过程。在这个过程中,每一个父view需要根据测量过程得到的尺寸值来合理地安排子view的大小和位置。


When a view's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that view's descendants. A view's measured width and measured height values must respect the constraints imposed by the view's parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent view may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small.

当一个view的measure()方法返回时,它的getMeasuredWidth() and getMeasuredHeight() 值必须被设定成功(感觉可以理解为它的getMeasuredWidth() and getMeasuredHeight() 方法都能有返回值了,也就是measure方法测量成功了。),同时该view的子view也已经被测量成功。一个view的测量后的宽度和高度必须服从它的父view给定的大小约束才行。这样才能保证在测量结束后,所有的父view能够接受它们所有的子控件。一个父view可能多次调用measure()来测量子view的尺寸。例如,父view第一次测量它的子view的尺寸来得到子view实际想要的大小,(如果这个子view的大小不符合要求,太大或太小)父view会第二次调用measure来重新确定子view的大小。



下面这一部分不翻译了。网络上很多人翻译得比我好多了,我这里就不误人子弟了。哈哈 

The measure pass uses two classes to communicate dimensions. The View.MeasureSpec class is used by views to tell their parents how they want to be measured and positioned. The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:

  • an exact number
  • MATCH_PARENT, which means the view wants to be as big as its parent (minus padding)
  • WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding).
There are subclasses of LayoutParams for different subclasses of ViewGroup. For example, AbsoluteLayout has its own subclass of LayoutParams which adds an X and Y value.

MeasureSpecs are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:

  • UNSPECIFIED: This is used by a parent to determine the desired dimension of a child view. For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child view wants to be given a width of 240 pixels.
  • EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
  • AT_MOST: This is used by the parent to impose a maximum size on the child. The child must gurantee that it and all of its descendants will fit within this size.

To intiate a layout, call requestLayout(). This method is typically called by a view on itself when it believes that is can no longer fit within its current bounds.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值