Android 你知道界面布局嵌套多少层之后会Crash吗

Posted aquarius

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 你知道界面布局嵌套多少层之后会Crash吗相关的知识,希望对你有一定的参考价值。

我们先放一张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吗的主要内容,如果未能解决你的问题,请参考以下文章

android界面设计之布局管理

Android布局层级过深为什么会对性能有影响?为什么Compose没有布局嵌套问题?

Android中布局层级过深为什么会对性能有影响?为什么 Compose 没有布局嵌套问题?

Android 布局优化

Android -- 每日一问:你是如何解决 Android 的布局嵌套问题的?

android重启项目之后布局界面打不开