Android -- View流程

來源:互聯網
上載者:User

標籤:

在自訂view中打log,view的顯示共有三種,visible、invisible和gone,分別看一下log:

gone
onVisibilityChangedconstruct 2 parametersonFinishInflateonVisibilityChangedonVisibilityChangedonRtlPropertiesChangedonRtlPropertiesChangedonAttachedToWindowonWindowVisibilityChangedonWindowFocusChanged
invisible
onVisibilityChangedconstruct 2 parameters .onFinishInflateonVisibilityChangedonVisibilityChangedonRtlPropertiesChangedonRtlPropertiesChangedonAttachedToWindowonWindowVisibilityChangedonMeasure , width : 1080  ; height: 1557onMeasure , width : 144  ; height: 1500onSizeChangedonLayout --> l: 0  ; r : 144  ; t: 57  ; b: 201  : changed :trueonMeasure , width : 1080  ; height: 1557onMeasure , width : 144  ; height: 1500onLayout --> l: 0  ; r : 144  ; t: 57  ; b: 201  : changed :falseonWindowFocusChanged
visible
construct 2 parameters .onFinishInflateonVisibilityChangedonVisibilityChangedonRtlPropertiesChangedonRtlPropertiesChangedonAttachedToWindowonWindowVisibilityChangedonMeasure , width : 1080  ; height: 1557onMeasure , width : 144  ; height: 1500onSizeChangedonLayout --> l: 0  ; r : 144  ; t: 57  ; b: 201  : changed :trueonMeasure , width : 1080  ; height: 1557onMeasure , width : 144  ; height: 1500onLayout --> l: 0  ; r : 144  ; t: 57  ; b: 201  : changed :falseonDrawonWindowFocusChanged

1、從中看到view預設為可見的,不是預設值時先調用 onVisibilityChanged ,但是此時該view的任何位置資訊都不知道。

2、可見度改變後才是調用帶有兩個參數的建構函式

3、從xml 檔案中 inflate 完成

4、將view 加到 window 中(View 是gone 的,那麼View建立生命週期也就結束)

5、測量view的長寬(onMeasure)

6、定位View在父View中的位置(onLayout)--->(View 是invisible ,View 建立生命週期結束)

7、onDraw(只有可見的View 才在window 中繪製)

setContentView(new View(this))
construct 1 parameteronVisibilityChangedonVisibilityChangedonRtlPropertiesChangedonRtlPropertiesChangedonAttachedToWindowonWindowVisibilityChangedonMeasure , width : 1080  ; height: 1557onSizeChangedonLayout --> l: 0  ; r : 1080  ; t: 0  ; b: 1557  : changed :trueonMeasure , width : 1080  ; height: 1557onLayout --> l: 0  ; r : 1080  ; t: 0  ; b: 1557  : changed :falseonDrawonWindowFocusChanged

預設情況下view的長和寬預設和父view的長和寬一致。

銷毀
onWindowFocusChanged
onWindowVisibilityChanged
onDetachedFromWindow
總結

View 的關鍵生命週期為

構造View --> onFinishInflate --> onAttachedToWindow --> onMeasure --> onSizeChanged --> onLayout --> onDraw --> onDetackedFromWindow

我是天王蓋地虎的分割線

Android -- View流程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.