android view源码(翻译头部3)


Layout

Layout is a two pass process: a measure pass and a layout pass.
布局是一个两阶段的过程:测量阶段和布局阶段。

The measuring
pass is implemented in {@link #measure(int, int)} and is a top-down traversal of the view tree.
测量过程是在{@link #measure(int,int)}中实现的,并且是视图树的自上向下遍历

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
{@link #layout(int,int,int,int)} and is also top-down.

   第二阶段发生在{@link #layout(int,int,int,int)}中,并且也是自上向下的

During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.

   在此过程中,每个父级负责使用度量过程中计算出的大小来定位其所有子级。

When a view's measure() method returns, its {@link #getMeasuredWidth()} and {@link #getMeasuredHeight()} values must be set, along with those for all of
that view's descendants.
当视图的measure()方法返回时,必须设置其{@link #getMeasuredWidth()}和{@link #getMeasuredHeight()}值,以及该视图所有后代的值。

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. 

   父视图可能对其子对象多次调用measure()

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.

例如,父母可以对每个孩子进行一次未指定尺寸的测量,以找出他们想要多大,然后,如果所有孩子不受约束的大小之和太大或太小,则用实际数字再次对他们调用measure()。

The measure pass uses two classes to communicate dimensions. 
测量传递使用两个类来传达大小

The
{@link MeasureSpec} class is used by views to tell their parents how they want to be measured and positioned.

   视图使用{@link MeasureSpec}类告诉其父级如何测量和定位。

The base LayoutParams class just describes how big the view wants to be for both width and height.

   基本的LayoutParams类仅描述了宽度和高度视图的大小

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)
MATCH_PARENT(这意味着视图希望与父视图一样大 减去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.
ViewGroup的不同子类有LayoutParams的子类。
For example, AbsoluteLayout has its own subclass of LayoutParams which adds
an X and Y value.

   例如,AbsoluteLayout有自己的LayoutParams子类,该子类添加了X和Y值。

MeasureSpecs are used to push requirements down the tree from parent to
child. A MeasureSpec can be in one of three modes:
MeasureSpecs用于将需求从父级到子级向下推。 MeasureSpec可以采用以下三种模式之一:

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.

   父母使用它来确定所需的子视图尺寸。
   例如,LinearLayout可以在其子级上调用measure(),将其高度设置为“UNSPECIFIED”,将宽度设置为EXACTLY 240,以了解如何

   子视图要设置为240像素的宽度。


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 guarantee that it and all of its descendants will fit
within this size.

   父母使用它来将最大大小强加给孩子。

   孩子必须保证自己及其所有后代都适合这个尺寸。

To initiate a layout, call {@link #requestLayout}. 
This method is typically
called by a view on itself when it believes that is can no longer fit within its current bounds.

要启动布局,请致电{@link #requestLayout}。

当认为不再适合当前范围的视图本身通常会调用此方法。
 


      关注本人公众号获取更多干货.


 

 

 

 




 

 

 

   

 

 

 
 
 
posted @ 2020-07-31 16:38  AndroidWang  阅读(115)  评论(0编辑  收藏  举报