Relative Layout

We've used RelativeLayout, often in combination with LinearLayout, throughout the MJAndroid application. The advantage of RelativeLayout is that you can express the a S BD« 8:56 PM


Upper Left


Upper Left

Figure 11-16. Same AbsoluteLayout in landscape mode relative positioning of the Views in the screen, and the layout manager will do its best to fit them all on the screen in the proper relations. An example follows: <?xml version="l.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView android:id="@+id/txtTextl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Textl" android:gravity="top" android:layout_alignParentRight="true"© />

<TextView android:id="@+id/txtText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text2" android:layout_below="@+id/txtTextl"© />

<Button android:id="@+id/btnButtonl"





<Button android:id="@+id/btnButton2" android:layout_width="150dp" android:layout_height="100dp" android:text="Button2"

android:layout_toRightOf="@+id/btnButton1"© android:layout_alignTop="@+id/btnButton1"©

</RelativeLayout> O Lays out Text1 at the top of the screen.

© Aligns Text1 with the right side of its parent (which is the screen itself). © Places Text2 below Text1. O Places Button1 below Text2. © Places Button2 just to the right of Button1. © Aligns the tops of the two buttons.

Figure 11-17 shows what this looks like in portrait mode (the emulator default), and Figure 11-18 shows it in landscape mode. The layout manager has adjusted the arrangements in each case to match the layout hints we gave in the XML.

G^BDfl 9:40 PM

G^BDfl 9:40 PM

Figure 11-17. RelativeLayout in portrait mode

Figure 11-18. RelativeLayout in landscape mode


0 0

Post a comment