线性和相对布局有什么区别?

时间:2011-03-22 05:13:26

标签: android layout

线性和相对布局有什么区别?

10 个答案:

答案 0 :(得分:13)

<强> LINEAR LAYOUT ::

  • 在线性布局中,如名称所示,所有元素都是 以线性方式显示
  • 水平或垂直,并设置此行为 android:orientation是节点的一个属性 的LinearLayout。
  • 线性布局将每个孩子一个接一个地排成一行, 水平或垂直。

Click here ---- for --- Android Docs reference for linear layout

Pictorial representation


<强> RELATIVE LAYOUT::

  • 在相对布局中,每个元素相对于其他元素排列 元素或父元素。
  • 在将视图添加到其他视图旁边时很有用
  • 使用相对布局,您可以为每个孩子提供一个LayoutParam 指定相对于父或者的确切位置 相对于其他孩子。
  • 视图在相对布局中相互叠加

Click here ---- for ---Android Docs reference for Relative layout

Pictorial representation


优化 :: 查看Optimizing Layout Hierarchies

视野越少,越好 ::

  1. The number one goal for your layouts should be using the fewest number of Views possible. The fewer Views you have to work with, the faster your application will run. Excessive nesting of Views further slows down your application.

  2. A RelativeLayout hierarchy will typically use fewer Views and have a flatter tree than a LinearLayout hierarchy. With LinearLayout, you must create a new LinearLayout every time you want to change the orientation of your views – creating additional Views and a more nested hierarchy. As a result, it is recommended that you first use RelativeLayout for any layout that has any complexity. There is a high probability you will reduce the number of Views – and the depth of your View tree – by doing so.

答案 1 :(得分:8)

线性布局将每个孩子一个接一个地放在一条线上,水平或垂直。使用相对布局,您可以为每个子项提供一个LayoutParam,它相对于父项或相对于其他子项的相对指定了应该去的确切位置。

答案 2 :(得分:4)

来自Android developer documentation: Common Layout Objects

<强>的LinearLayout

  

LinearLayout在一个方向上对齐所有子项 - 垂直或水平,具体取决于您定义方向属性的方式。

<强> RelativeLayout的

  

RelativeLayout让子视图指定它们相对于父视图的位置或彼此之间的位置(由ID指定)

答案 3 :(得分:0)

以下链接应直观地解释布局如何“视觉”工作 http://www.droiddraw.org/
将一些组件添加到窗口并混淆布局以查看发生了什么,这就是我学习每个组件所做的事情。

答案 4 :(得分:0)

Android中LinearLayout的{​​{3}}功能之一是使用名为权重的属性,该应用可以使用android:layout_weight指定。 此属性根据屏幕应占用的空间为视图指定“重要性”值。

另一方面,RelativeLayout不支持体重,换句话说,RelativeLayout不关注android:layout_weight。这是LinearLayout.LayoutParams的属性,但不是RelativeLayout.LayoutParams的属性。

答案 5 :(得分:0)

在相对布局中,布局页面中的所有内容都与example_layout.xml页面中的其他内容相关

在线性布局的情况下,元素以线性格式显示

答案 6 :(得分:0)

android中的线性和相对布局之间的区别在于,在线性布局中,“子”可以水平或垂直放置,但是,在相对布局中,子项可以彼此相对距离放置。这是线性布局和相对布局之间的差异。

答案 7 :(得分:0)

区别很简单:在LinearLayout中,我们以线性方式(一个接一个)排列东西,而在RelativeLayout中,我们可以将东西放在屏幕上的任何位置。

=&GT;线性布局按列表排列。 休息它们的功能相似。

答案 8 :(得分:0)

线性布局

  1. 线性布局非常适合在行和列中对齐视图。
  2. 使用布局权重来划分一个地方是一种很好的方法,这些权重会根据展示尺寸扩大或缩小视图。
  3. 相对布局

    1. 相对布局非常适合相对于彼此定位元素。
    2. 例如,将B放在A下方或将C放在左下角。Check the Screen shoot
    3. 相对布局也可以轻松重叠视图。例如:视图A是重叠视图B. Check the Screen-Shoot

答案 9 :(得分:0)

RelativeLayoutLinearLayout更灵活,但是如果您对LinearLayout有适当的了解,也可以使用。 对于LinearLayout,每个属性都有一个重要位置,由开发人员进行硬编码。 对于RelativeLayout,您可以通过与他人属性关联来更改位置。

相关问题