【问题标题】:What is the difference between linear and relative layout?线性布局和相对布局有什么区别?
【发布时间】:2020-11-14 19:00:08
【问题描述】:

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

【问题讨论】:

    标签: android layout


    【解决方案1】:

    LINEAR LAYOUT ::

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

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


    RELATIVE LAYOUT::

    • 在相对布局中,每个元素都相对于其他元素进行排列 元素或父元素。
    • 在添加视图并排放置等时很有帮助
    • 使用相对布局,您可以为每个孩子分配一个 LayoutParam 确切地指定应该去哪里,相对于父母或 相对于其他孩子。
    • 视图在相对布局中相互叠加

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


    优化::看看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.

    【讨论】:

      【解决方案2】:

      线性布局将每个孩子一个接一个地排成一行,水平或垂直。使用相对布局,您可以为每个孩子提供一个 LayoutParam,该 LayoutParam 指定确切的位置,相对于父级或相对于其他子级。

      【讨论】:

        【解决方案3】:

        来自Android developer documentation: Common Layout Objects

        线性布局

        LinearLayout 将所有子级对齐在一个方向 - 垂直或水平,具体取决于您如何定义方向属性。

        相对布局

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

        【讨论】:

          【解决方案4】:

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

          => 线性布局以列表形式排列。 其余的它们在功能上是相似的。

          【讨论】:

          • 详细说明你的答案
          【解决方案5】:

          Android 中LinearLayoutcharacteristic 功能之一是使用名为Weight 的属性,应用可以使用android:layout_weight 指定该属性。 此属性根据视图应在屏幕上占据多少空间为视图分配一个“重要性”值。

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

          【讨论】:

            【解决方案6】:

            线性布局

            1. 线性布局非常适合在行和列中对齐视图。
            2. 它们是一种使用布局权重划分一个位置的好方法,布局权重会根据显示器的大小扩大或缩小视图。

            相对布局

            1. 相对布局非常适合相对于彼此定位元素。
            2. 例如将 B 放在 A 下方或将 C 放在左下角。Check the Screen shoot
            3. 相对布局也使重叠视图变得容易。例如:视图 A 与视图 B 重叠。Check the Screen-Shoot

            【讨论】:

              【解决方案7】:

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

              【讨论】:

              • 嗨穆拉德。这是评论而不是真正的答案。另外,这个问题已经有很多回答了。请尽量避免提供额外的问题答案,除非有一个独特的解决方案可以呈现给社区。 Some tips for answering here 最后,RelativeLayout 被 Android Studio 归类为“遗留”布局,因此开发人员应避免使用它,而应使用 ConstraintLayout,除非他们正在维护一个过时的遗留项目。亲切的问候。
              【解决方案8】:

              以下链接应直观地解释布局如何“直观地”工作
              http://www.droiddraw.org/
              将一些组件添加到窗口并弄乱布局,看看会发生什么,这就是我了解每个组件的作用。

              【讨论】:

                【解决方案9】:

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

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

                【讨论】:

                  【解决方案10】:

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-04-29
                    • 1970-01-01
                    • 2012-07-06
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多