在Android中,哪种布局更快?1. LinearLayout内的FrameLayouts或2.大的RelativeLayout? [英] In Android, what Layout is faster 1. FrameLayouts inside LinearLayout or 2. One big RelativeLayout?

查看:191
本文介绍了在Android中,哪种布局更快?1. LinearLayout内的FrameLayouts或2.大的RelativeLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为视图考虑两种不同的布局安排(在RecyclerView中将是一行).它们是不同的,但是它们都可以工作,或者

  1. 一个容器垂直LinearLayout,其中包含FrameLayouts的行,例如LinearLayout-> FrameLayouts->内容
  2. 一个大的RelativeLayout,其中包含所有其他视图(不包含FrameLayout,但包含所有TextView,Button等),例如RelativeLayout->内容

现在,我想知道:从布局性能上看,哪一个更可取?

据我了解,RelativeLayout必须进行两次测量,因此非常慢,而就布局性能而言,FrameLayout很快而LinearLayout很好.在一种情况下,我只有一个速度较慢的容器(RelativeLayout-> Content),而在另一个情况下,我将具有两个速度较快的容器(LinearLayout-> FrameLayouts-> Content).

解决方案

简答(TLDR):

如果您真的知道自己在做什么,则RelativeLayouts可以更快. 如果您不这样做,它们的速度可能会慢很多.

长答案:

手动嵌套布局(例如LinearLayout中的FrameLayout与在RelativeLayout中使用相对定位)可以有正反两方面.

代价是计算动态尺寸,例如"wrap_content",这些尺寸必须根据孩子的需求而扩展,或者由于父母的要求而受到限制.这会导致阶乘计算问题,该问题会随着深度的增加而增加.

通过使用嵌套视图进行手动定位可以提高级别.级别越浅,效果越好,但仍然有效.因此,从字面上看,工作量是如上所述的关卡深度的阶乘. 例如: A嵌套在根"下. B嵌套在A下. C嵌套在B下. 一个会影响根的宽度. B影响A,而A影响根. C会影响B,B会影响A,而A会影响根.

在动态尺寸计算方面,通过使一个视图本身相对于另一视图本身的相对定位与嵌套视图相同,因为必须计算每个关系时都要考虑另一个关系. 例如:

A离开根. B在A的左边. C在B的左边. 当A,B,C都生活在Root下时,动态测量将如下发生: 一个会影响根的宽度. B影响A,而A影响根. C会影响B,B会影响A,而A会影响根.

换句话说,您进行了相同的阶乘计算.

因此,唯一的真正区别是RelativeLayouts可以让您更好地控制相对位置,但以更复杂的XML为代价.

另一方面,通过谨慎使用根据设备属性提前计算的@dimens,可以避免动态计算,并且在正确使用RelativeLayouts时,与其他布局相比,在需要复杂定位时,其性能要高得多

I am considering two different layout arrangements for a view (which will be a row in a RecyclerView). They are different, but they will both work, either as

  1. One container vertical LinearLayout containing rows of FrameLayouts e.g. LinearLayout -> FrameLayouts -> Content
  2. One big RelativeLayout that contains all other views (no FrameLayouts but all TextViews, Buttons and such) e.g. RelativeLayout -> Content

Now I was wondering: which one would be more preferable in terms of Layout performance?

It was my understanding that a RelativeLayout has to be measured twice and thus is pretty slow, while a FrameLayout is fast and a LinearLayout is ok in terms of layout performance. In one case I would only have one container that is slow (RelativeLayout -> Content) in the other I would have two containers that are quicker (LinearLayout -> FrameLayouts -> Content).

解决方案

Short Answer (TLDR):

If you really know what you're doing, RelativeLayouts can be faster. If you don't, they can be much slower.

Long Answer:

Nesting layouts manually such as a FrameLayout within a LinearLayout versus using relative positioning in a RelativeLayout can have pros and cons either way.

The cost is in calculating the dynamic sizes such as "wrap_content" which must expand based on their children's needs or constrain due to parent's requirements. This causes a factorial calculation problem which increases with depth.

Manual positioning by using nested views increases levels. The shallower the level, the better but still work. So literally the amount of work is the factorial of your level depth as described above. For example: A is nested under Root. B is nested under A. C is nested under B. A affects Root's width. B affects A which affects Root. C affects B which affects A which affects Root.

Relative positioning by having one view position itself relative to another is the same as nested views in terms of dynamic sizing calculations since each relationship must be calculated with consideration of the other. For example:

A is left of Root. B is left of A. C is left of B. While A,B,C all live under Root, the dynamic measurement occurs like so: A affects Root's width. B affects A which affects Root. C affects B which affects A which affects Root.

In other words, you have the same factorial calculation occurring.

So the only real difference is that RelativeLayouts give you finer control over relative positions at the cost of more complex XML.

On the flip side, with careful use of @dimens that are calculated ahead of time based on device attributes, the dynamic calculations can be avoided and when used properly, RelativeLayouts can be much more performant than any other layout when needing complex positioning.

这篇关于在Android中,哪种布局更快?1. LinearLayout内的FrameLayouts或2.大的RelativeLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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