码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-10-28 00:30:25      阅读:273      评论:0      收藏:0      [点我收藏+]

标签: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吗

标签:hdr   pre   flow   open   界面   alt   pen   omv   star   

原文地址:http://www.cnblogs.com/sphere/p/7745687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!