zoukankan      html  css  js  c++  java
  • 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流畅度。

    特别声明:如果转载,请保留出处信息,感谢您的关注和推荐!!
  • 相关阅读:
    深入理解Java内存(图解堆栈)
    java堆栈区别
    Java之堆栈的区别
    【微信小程序】获取轮播图当前图片下标、滑动展示对应的位数、点击位数展示对应图片
    【微信小程序】转载:微信小程序实战篇-下拉刷新与加载更多
    【微信小程序】转载:微信小程序之购物车功能
    【微信小程序】loading标签使用,可自定义时长
    【微信小程序】日历插件,适用于酒店订房类小程序
    【微信小程序】微信小程序wx.previewImage预览图片
    【微信小程序】小程序和公众号 退款功能教程(含申请退款和退款回调,退款回调地址在商户后台配置或者代码自定义)
  • 原文地址:https://www.cnblogs.com/sphere/p/7745687.html
Copyright © 2011-2022 走看看