1. 程式人生 > 實用技巧 >android view原始碼(翻譯頭部3)

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可以採用以下三種模式之一: