Relative layouts
Probably the biggest advantage of the relative layout is the way it can be used to reduce the number of nested view groups when building complex layouts. This works by defining views' positions in accordance to how they are positioned and aligned to each other with properties such as layout_below
and layout_toEndOf
. To see how this is done, consider the linear layout of the previous example. We can recreate this as a relative layout with no nested viewgroups, like so:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/header" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> ...