標籤:控制項 any jsb raw tsp view設定 構造 聯絡 lda
前言
瞭解ViewGroup的生命週期的執行步驟對於自己自訂ViewGroup的時候十分重要,清楚了整個流程才能對ViewGroup有更深的理解。本文從個人的總結,來闡述一下執行的順序。
執行說明
首先ViewGroup的常用的生命週期主要有:構造方法、onLayout()、onFinishInflate()、onMeasure()、onSizeChanged(),前兩種在建立ViewGroup子類的時候,必須重寫。至於draw()和drawChild()是其用來繪製背景和子View用的,就不在生命週期裡一一敘述。
第一種:在xml裡直接引用的,執行順序一般是:構造方法->onFinishInflate()(只執行一次)->onMeasure()(可能多次執行)->onSizeChanged()(在重新onMeasure的時候發現跟之前測量的尺寸不一樣的時候就會回調此方法)->onLayout()(布置子View)->onMeasure()->onLayout().......
第二種:在Activity中setContentView( newCustomView(this))引用的,執行順序與第一種相比,除了構造方法引用的不一致和不執行onFinishInflate()外,其他基本一致。
第三種:在Activity中直接new CustomView(this)而且不添加任何父布局的時候只會執行構造方法,其它不會執行。
總結技巧:
onMeasure()裡一般是定義子控制項的測量尺寸和寬高。
首先設定ViewGroup自身的尺寸如下:
int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(widthSize, heightSize);
然後設定子View的尺寸例如下面:
View leftMenuView = getChildAt(1); MarginLayoutParams lp = (MarginLayoutParams) leftMenuView.getLayoutParams(); final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, mMinDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width); final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin, lp.height); leftMenuView.measure(drawerWidthSpec, drawerHeightSpec); View contentView = getChildAt(0); lp = (MarginLayoutParams) contentView.getLayoutParams(); final int contentWidthSpec = MeasureSpec.makeMeasureSpec( widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY); final int contentHeightSpec = MeasureSpec.makeMeasureSpec( heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY); contentView.measure(contentWidthSpec, contentHeightSpec);
int getChildMeasureSpec(int spec,int padding,int childDimension) 返回的是測量尺寸規格spec,可以給子View設定padding,不需要設定padding就直接如contentView一樣。
onLayout()布局子view的位置,基本上用到layout(left,top,right,bottom);
getWidth()和getMeasureWidth()的區別與聯絡:
getMeasuredWidth(): 只要一執行完 setMeasuredDimension() 方法,就有值了,並且不再改變。簡單來說執行完onMeasure裡的方法後就可以擷取;
getWidth()只有在執行onMeasure()之後才能擷取,但是可能應為布局大小調整發生變化,如果onLayout()沒有對子View的寬高進行修改,那麼兩個值相等。
生命週期表:
參考:http://blog.csdn.net/anydrew/article/details/50985763
android 自訂控制項之ViewGroup生命週期執行步驟