Android 线性布局与相对布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11356413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LinearLayout vs RelativeLayout
提问by Soumyadip Das
What are the advantages of RelativeLayout
over LinearLayout
in android ? For a particular design which one would you prefer and what's the reason behind of that ??
RelativeLayout
overLinearLayout
在android中的优势是什么?对于特定的设计,您更喜欢哪种设计,其背后的原因是什么?
Is it(RelativeLayout) comparable or similar like HTML <div>
??
它(RelativeLayout)是否与 HTML 类似或相似<div>
?
采纳答案by BlackHatSamurai
Read this article:
阅读这篇文章:
The Android UI toolkit offers several layout managers that are rather easy to use and, most of the time, you only need the basic features of these layout managers to implement a user interface. Sticking to the basic features is unfortunately not the most efficient way to create user interfaces. A common example is the abuse of LinearLayout, which leads to a proliferation of views in the view hierarchy. Every view, or worse every layout manager, you add to your application comes at a cost: initialization, layout and drawing become slower. The layout pass can be especially expensive when you nest several LinearLayout that use the weightparameter, which requires the child to be measured twice...
In a RelativeLayout, views are aligned either with their parent, the RelativeLayout itself, or other views. For instance, we declared that the description is aligned with the bottom of the RelativeLayout and that the title is positioned above the description and anchored to the parent's top. With the description GONE, RelativeLayout doesn't know where to position the title's bottom edge. To solve this problem, you can use a very special layout parameter called alignWithParentIfMissing.
This boolean parameter simply tells RelativeLayout to use its own edges as anchors when a constraint target is missing. For instance, if you position a view to the right of a GONE view and set alignWithParentIfMissing to true, RelativeLayout will instead anchor the view to its left edge. In our case, using alignWithParentIfMissing will cause RelativeLayout to align the title's bottom with its own bottom.
...the difference will be much more important when you use such a layout for every item in a ListView for instance...
Android UI 工具包提供了几个相当易于使用的布局管理器,大多数情况下,您只需要这些布局管理器的基本功能即可实现用户界面。不幸的是,坚持基本功能并不是创建用户界面的最有效方法。一个常见的例子是滥用LinearLayout,这导致视图层次结构中的视图激增。添加到应用程序中的每个视图,或者更糟的每个布局管理器都是有代价的:初始化、布局和绘制变得更慢。当您嵌套多个使用权重参数的LinearLayout 时,布局传递可能特别昂贵,这需要对子项进行两次测量...
在RelativeLayout 中,视图与其父视图、RelativeLayout 本身或其他视图对齐。例如,我们声明描述与 RelativeLayout 的底部对齐,并且标题位于描述上方并锚定到父级的顶部。使用描述 GONE 时,RelativeLayout 不知道将标题的底部边缘放在哪里。为了解决这个问题,您可以使用一个非常特殊的布局参数alignWithParentIfMissing。
这个布尔参数只是告诉 RelativeLayout 在缺少约束目标时使用它自己的边缘作为锚点。例如,如果您将视图放置在 GONE 视图的右侧并将 alignWithParentIfMissing 设置为 true,RelativeLayout 会将视图锚定到其左边缘。在我们的例子中,使用 alignWithParentIfMissing 将导致 RelativeLayout 将标题的底部与其自己的底部对齐。
...例如,当您为 ListView 中的每个项目使用这样的布局时,差异将变得更加重要...
回答by AkashG
By the name you can come to know LinearLayout
adds view linearly either vertical or horizontal by the orientation set by you. It will add views one after another which will depend on the requirement of the design. And RelativeLayout
adds view related with each other,there is no need to declare orientation in RelativeLayout
.
顾名思义,您可以知道LinearLayout
根据您设置的方向线性地添加垂直或水平视图。它将一个接一个地添加视图,这取决于设计的要求。并且RelativeLayout
添加了相互关联的视图,不需要在RelativeLayout
.
As for example you want to add two TextView
s one under another so you will add first TextView
and refer the second TextView
with the first TextView
by adding android:layout_below="first textview's id"
in the XML file. This way you can deal with RelativeLayout
.
例如,您想TextView
在另一个下添加两个,因此您将首先添加TextView
并通过在 XML 文件中添加来引用第二个TextView
和第一个。这样你就可以处理了。TextView
android:layout_below="first textview's id"
RelativeLayout