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" ...
Get Android Design Patterns and Best Practice now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.