1.在API中對View的回調流程有一個詳細的描述,下面給出了原文翻譯:
1.Creation:創建
1.1----Constructors(構造器)
在構造器中有一個表單負責View從代碼中創建和從Layout File 文件中創建,第二個表單負責解析和應用一些在Layout File中定義的屬性。
默認的佈局文件調用的是兩個參數的構造方法
1.2---- onFinishInflate()
當View和他的所有子View從XML中解析完成後調用。
這個是當系統解析XML完成,並且將子View全部添加完成之後調用這個方法,我們通常重寫這個方法,在這個方法中查找並獲得子View引用
@Override
protected void onFinishInflate() {
mHandle = findViewById(mHandleId);
if (mHandle == null) {
throw new IllegalArgumentException("The handle attribute is must refer to an"
+ " existing child.");
}
mHandle.setOnClickListener(new DrawerToggler());
mContent = findViewById(mContentId);
if (mContent == null) {
throw new IllegalArgumentException("The content attribute is must refer to an"
+ " existing child.");
}
mContent.setVisibility(View.GONE);
}
1.3-----onAttachedToWindow
當view黏附在當前window時調用
1.4----onWindowVisibilityChanged(int)
在當前window中的view發生改變時調用
2.
Layout :佈局
2.1----onMeasure(int, int)
確定View和它所有的子View要求的尺寸時調用
測量這個View的高和寬。通過調用這個方法來設置View的測量後的高和寬,其最終調用的方法是:
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
mMeasuredWidth = measuredWidth;
mMeasuredHeight = measuredHeight;
mPrivateFlags |= MEASURED_DIMENSION_SET;
}
可見其最終是將高和寬保存在mMeasuredWidth、mMeasuredHeight這兩個參數中。
其實調用onMeasure(int, int)的方法的不是系統,而是 public final voidmeasure(int widthMeasureSpec, int heightMeasureSpec)
2.2----onLayout(boolean, int, int,int, int)
當這個View爲其所有的子View指派一個尺寸和位置時調用
2.3---- onSizeChanged(int, int, int,int)
當這個View的尺寸改變後調用
3.
Drawing :繪製
3.1---- onDraw(Canvas)
當View渲染其內容時調用
4.Event processing :事件流程
4.1----onKeyDown(int, KeyEvent)
當一個新的鍵按下時
4.2 ---- onKeyUp(int, KeyEvent)
當一個鍵彈起時
4.3----onTrackballEvent(MotionEvent)
當滾跡球事件發生時。
4.4----onTouchEvent(MotionEvent)
當一個觸摸屏事件發生時。
5.Focus :焦點
5.1 ---- onFocusChanged(boolean, int,Rect)
當View得到和失去焦點時調用
5.2---- onWindowFocusChanged(boolean)
當Window包含的View得到或失去焦點時調用。
6.Attaching 依賴onWindowVisibilityChanged(int)
6.1----onWindowVisibilityChanged(int)
在當前window中的view發生改變時調用
6.2----onDetachedFromWindow()
當view從當前window去掉時調用