相对布局比线性布局更昂贵吗? [英] Is a RelativeLayout more expensive than a LinearLayout?

查看:29
本文介绍了相对布局比线性布局更昂贵吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我需要一个 View 容器时,我总是使用 RelativeLayout,因为它很灵活,即使我只想显示一些非常简单的东西.

I've always been using RelativeLayout everytime I needed a View container, because of it's flexibility, even if I just wanted to display something really simple.

这样做可以吗,或者我应该尽可能尝试使用 LinearLayout,从性能/良好实践的角度来看?

Is it ok to do so, or should I try using a LinearLayout when I can, from a performance/good practices standpoint?

谢谢!

推荐答案

在 2013 年 Google I/O 大会(为 Android 编写自定义视图)的一次演讲中,Romain Guy 澄清了导致每个人都开始使用 RelativeLayouts 的误解.相对布局始终必须执行两次测量传递.总的来说,只要您的视图层次结构简单,它就可以忽略不计.但是,如果您的层次结构很复杂,则进行额外的测量传递可能会相当昂贵.此外,如果您嵌套 RelativeLayouts,则会得到指数测量算法.

In a talk at Google I/O 2013 (Writing Custom Views for Android), Romain Guy clarified the misunderstanding that caused everyone to start using RelativeLayouts for everything. A RelativeLayout always has to do two measure passes. Overall it is negligible as long as your view hierarchy is simple. But if your hierarchy is complex, doing an extra measure pass could potentially be fairly costly. Also if you nest RelativeLayouts, you get an exponential measurement algorithm.

https://www.youtube.com/watch?v=NYtB6mlu7vA&t=1m41s

https://www.youtube.com/watch?v=NYtB6mlu7vA&t=38m04s

这篇关于相对布局比线性布局更昂贵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆