【问题标题】:Is it okay to nest several relative layouts嵌套几个相对布局可以吗
【发布时间】:2013-10-11 11:46:07
【问题描述】:

我已经创建了这个布局,它使用了几个嵌套的相对布局,我看到一些帖子说应该使用相对布局而不是嵌套线性布局。

嵌套相对布局怎么样?像这样嵌套布局有什么缺点(下面的xml代码)

如果有任何缺点,谁能推荐一个更好的方法来做到这一点?提前致谢。

Xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

<TextView
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />

<RelativeLayout
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/view1"
    android:gravity="center" >

    <TextView
        android:id="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/view3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_toRightOf="@+id/view2" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/layout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/layout1"
    android:gravity="center" >

    <TextView
        android:id="@+id/view4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/view5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_toRightOf="@+id/view4" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/layout3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/layout2"
    android:gravity="center" >

    <TextView
        android:id="@+id/view6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/view7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_toRightOf="@+id/view6" />
</RelativeLayout>

</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-relativelayout nestedlayout


    【解决方案1】:

    在 Google I/O 2013(为 Android 编写自定义视图)上的一次演讲中,Romain Guy 澄清了导致每个人都开始使用 RelativeLayouts 的误解。一个RelativeLayout 总是必须做两次测量传递。总的来说,只要您的视图层次结构简单,它就可以忽略不计。但是,如果您的层次结构很复杂,那么进行额外的度量传递可能会相当昂贵。此外,如果您嵌套 RelativeLayouts,您将获得指数测量算法。

    https://www.youtube.com/watch?v=NYtB6mlu7vA&t=1m41s

    https://www.youtube.com/watch?v=NYtB6mlu7vA&t=38m04s

    https://developers.google.com/events/io/sessions/325615129

    您可以使用 Android SDK 附带的工具来衡量您的 UI 性能。看看那个链接:http://developer.android.com/training/improving-layouts/optimizing-layout.html

    【讨论】:

    • 测量通过是什么意思?
    • 这种情况下可以考虑操作。因此,相对布局总是必须执行两个操作来计算其水平和垂直大小,并且由于线性布局始终具有固定的方向,因此它只执行一个操作。
    【解决方案2】:

    据我了解,这样做并没有错,尽管您应该尽可能避免嵌套布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多