The Relative Layout Layout Manager

Another interesting layout manager is the RelativeLayout. As the name suggests, this layout manager implements a policy where the controls in the container are laid out relative to either the container or another control in the container. Listing 4-32 and Figure 4-17 show an example.

Listing 4-32. Using a RelativeLayout Layout Manager

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content">

<TextView android:id="@+id/userNameLbl"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Username: " android:layout_alignParentTop="true" />

<EditText android:id="@+id/userNameText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/userNameLbl" />

<TextView android:id="@+id/pwdLbl"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/userNameText" android:text="Password: " />

<EditText android:id="@+id/pwdText"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/pwdLbl" />

<TextView android:id="@+id/pwdHintLbl"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/pwdText" android:text="Password Criteria... " />

<TextView android:id="@+id/disclaimerLbl" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"

Figure 4-17. A UI laid out using the RelativeLayout layout manager

As shown, the user interface looks like a simple login form. The username label is pinned to the top of the container because we set android:layout_alignParentTop to true. Similarly, the username input field is positioned below the username label because we set android:layout_below. The password label appears below the username label, the password input field appears below the password label, and the disclaimer label is pinned to the bottom of the container because we set android:layout_alignParentBottom to true.

Working with RelativeLayout is fun due to its simplicity. In fact, once you start using it, it'll become your favorite layout manager—you'll find yourself going back to it over and over again.

android:text="Use at your own risk... " />

</RelativeLayout>

Figure 4-17. A UI laid out using the RelativeLayout layout manager

0 0

Post a comment