【发布时间】:2020-11-14 19:00:08
【问题描述】:
线性布局和相对布局有什么区别?
【问题讨论】:
线性布局和相对布局有什么区别?
【问题讨论】:
LINEAR LAYOUT ::
Click here ---- for --- Android Docs reference for linear layout
RELATIVE LAYOUT::
Click here ---- for ---Android Docs reference for Relative layout
优化::看看Optimizing Layout Hierarchies
观看次数越少越好::
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.
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.
【讨论】:
线性布局将每个孩子一个接一个地排成一行,水平或垂直。使用相对布局,您可以为每个孩子提供一个 LayoutParam,该 LayoutParam 指定确切的位置,相对于父级或相对于其他子级。
【讨论】:
来自Android developer documentation: Common Layout Objects
线性布局
LinearLayout 将所有子级对齐在一个方向 - 垂直或水平,具体取决于您如何定义方向属性。
相对布局
RelativeLayout 让子视图指定它们相对于父视图或彼此之间的位置(由 ID 指定)
【讨论】:
区别很简单:在 LinearLayout 中,我们以线性方式(一个接一个)排列内容,而在 RelativeLayout 中,我们可以将内容放置在屏幕上的任何位置。
=> 线性布局以列表形式排列。 其余的它们在功能上是相似的。
【讨论】:
Android 中LinearLayout 的characteristic 功能之一是使用名为Weight 的属性,应用可以使用android:layout_weight 指定该属性。
此属性根据视图应在屏幕上占据多少空间为视图分配一个“重要性”值。
另一方面,RelativeLayout 不支持重量,换句话说,RelativeLayout 不关注android:layout_weight。这是LinearLayout.LayoutParams 的属性,但不是RelativeLayout.LayoutParams 的属性。
【讨论】:
线性布局
相对布局
【讨论】:
RelativeLayout 比LinearLayout 更灵活,但如果您对LinearLayout 有适当的了解,您也可以使用它。
对于LinearLayout,每个属性都有一个由开发人员硬编码的重要位置。
对于RelativeLayout,可以通过关联其他属性来改变位置。
【讨论】:
以下链接应直观地解释布局如何“直观地”工作
http://www.droiddraw.org/
将一些组件添加到窗口并弄乱布局,看看会发生什么,这就是我了解每个组件的作用。
【讨论】:
在相对布局中,布局页面中的所有内容都与example_layout.xml页面中的其他内容相关
在线性布局的情况下,元素以线性格式显示
【讨论】:
android中线性布局和相对布局的区别在于,在线性布局中,“孩子”可以水平或垂直放置,但是,在相对布局中,孩子可以相对距离放置。这是线性布局和相对布局之间的区别。
【讨论】: