我们先放一张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
在API23 Android6.0上嵌套层数可以更多,实验了四十多层还依旧没有crash
至于具体低于4.4的版本是什么样的结果,我并没有测试,且Android开源各大手机厂商可能会修改逻辑。不过考虑到向下兼容的问题,还是不应该嵌套太多层数,
当自己写的布局层级超过10层的时候就该考虑优化布局了,因为即使应用不因为嵌套的太多而crash,也会造成渲染时间过长等问题,影响UI流畅度。