我們先放一張Hierarchy Viewer的圖:(模擬器Android4.4) 看到數字6了嗎,那個RelativeLayout是MainActivity的根ViewGroup, 而在RelativeLayout之前還有5層, 即系統預設的界面DecorView(FrameLayout) --> ...
我們先放一張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.ViewView Code
在API23 Android6.0上嵌套層數可以更多,實驗了四十多層還依舊沒有crash
至於具體低於4.4的版本是什麼樣的結果,我並沒有測試,且Android開源各大手機廠商可能會修改邏輯。不過考慮到向下相容的問題,還是不應該嵌套太多層數,
當自己寫的佈局層級超過10層的時候就該考慮優化佈局了,因為即使應用不因為嵌套的太多而crash,也會造成渲染時間過長等問題,影響UI流暢度。