Android 你知道介面布局嵌套多少層之後會Crash嗎

來源:互聯網
上載者:User

標籤:hdr   pre   flow   open   介面   alt   pen   omv   star   

我們先放一張Hierarchy Viewer的圖:(模擬器Android4.4)

 

看到數字6了嗎,那個RelativeLayout是MainActivity的根ViewGroup, 而在RelativeLayout之前還有5層, 即系統預設的介面DecorView(FrameLayout) --> LinearLayout --> FramelLayout-- >ActionBarOverlayLayout --> ContentFrameLayout.

 直接說結果吧,用模擬器在API19  也就是Android4.4 上最大可以嵌套39層,包含系統預設的5層,也就說我們最大可以寫34層的嵌套。

當嵌套層數達到40層時報如下異常,

10-27 11:38:53.901 25582-25582/com.aquarius.customview E/AndroidRuntime: FATAL EXCEPTION: main                                                                         Process: com.aquarius.customview, PID: 25582                                                                         java.lang.StackOverflowError                                                                             at android.util.Pools$SynchronizedPool.acquire(Pools.java:154)                                                                             at android.view.GLES20RecordingCanvas.obtain(GLES20RecordingCanvas.java:43)                                                                             at android.view.GLES20DisplayList.start(GLES20DisplayList.java:61)                                                                             at android.view.View.getDisplayList(View.java:13323)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)                                                                             at android.view.View.getDisplayList(View.java:13357)                                                                             at android.view.View.getDisplayList(View.java:13404)                                                                             at android.view.View.draw(View.java:14182)                                                                             at android.view.ViewGroup.drawChild(ViewGroup.java:3103)                                                                             at android.view.View
View Code


在API23 Android6.0上嵌套層數可以更多,實驗了四十多層還依舊沒有crash

至於具體低於4.4的版本是什麼樣的結果,我並沒有測試,且Android開源各大手機廠商可能會修改邏輯。不過考慮到向下相容的問題,還是不應該嵌套太多層數,

當自己寫的布局層級超過10層的時候就該考慮最佳化布局了,因為即使應用不因為嵌套的太多而crash,也會造成渲染時間過長等問題,影響UI流暢度。

 

Android 你知道介面布局嵌套多少層之後會Crash嗎

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.