androidandroid-layoutandroid-relativelayoutnestedlayout

Is it okay to nest several relative layouts


I've created this layout and it uses several nested relative layouts, i've seen some posts saying relative layouts should be used instead nesting linear layouts.

How about nesting relative layouts? What are the downsides to nesting layouts like this (xml code below)

If there are any downsides, could anyone recommend a better way to do this? Thanks in advance.

enter image description here

Xml code:

<?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>

Solution

  • In a talk at Google I/O 2013 (Writing Custom Views for Android), Romain Guy clarified the misunderstanding that caused everyone to start using RelativeLayouts for everything. A RelativeLayout always has to do two measure passes. Overall it is negligible as long as your view hierarchy is simple. But if your hierarchy is complex, doing an extra measure pass could potentially be fairly costly. Also if you nest RelativeLayouts, you get an exponential measurement algorithm.

    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

    You can measure your UI performance using tools that came with Android SDk. Take a look on that link: http://developer.android.com/training/improving-layouts/optimizing-layout.html