Android 你知道界面布局嵌套多少层之后会Crash吗_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android 你知道界面布局嵌套多少层之后会Crash吗

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

 2017/10/28 3:11:28  sphere  程序员俱乐部  我要评论(0)
  • 摘要:我们先放一张HierarchyViewer的图:(模拟器Android4.4)看到数字6了吗,那个RelativeLayout是MainActivity的根ViewGroup,而在RelativeLayout之前还有5层,即系统默认的界面DecorView(FrameLayout)-->LinearLayout-->FramelLayout-->ActionBarOverlayLayout-->ContentFrameLayout.直接说结果吧
  • 标签:android

我们先放一张Hierarchy Viewer的图:(模拟器Android4.4)

 

看到数字6了吗,那个RelativeLayout是MainActivity的根ViewGroup, 而在RelativeLayout之前还有5层, 即系统默认的界面DecorView(FrameLayout) --> LinearLayout --> FramelLayout-- >ActionBarOverlayLayout --> ContentFrameLayout.

 直接说结果吧,用模拟器在API19  也就是Android4.4 上最大可以嵌套39层,包含系统默认的5层,也就说我们最大可以写34层的嵌套。

当嵌套层数达到40层时报如下异常

class="code_img_closed" src="/Upload/Images/2017102803/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('c0db2f86-312b-4317-9007-e41423970862',event)" src="/Upload/Images/2017102803/2B1B950FA3DF188F.gif" alt="" />
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流畅度。

 

发表评论
用户名: 匿名