Tuesday, August 9, 2011

RelativeLayout

Android's RelativeLayout is the best thing since Swing's GridBagLayout.

The training I went through (and most of the online material I looked at) used mostly LinearLayout -- and, in fact, that's what the Android Eclipse plugin defaults to when you create a new layout XML file.

Far better to start out with RelativeLayout.  It looks like this:
<relativelayout 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android">
    

<textview android:id="@+id/textView1" 
android:layout_alignparentleft="true" 
android:layout_alignparenttop="true" 
android:layout_height="wrap_content" 
android:layout_marginleft="68dp" 
android:layout_margintop="75dp" 
android:layout_width="wrap_content" 
android:text="TextView">
</textview>
    <textview 
android:id="@+id/textView2" 
android:layout_alignleft="@+id/textView1" 
android:layout_below="@+id/textView1" 
android:layout_height="wrap_content" 
android:layout_margintop="42dp" 
android:layout_width="wrap_content" 
android:text="TextView"></textview>

</relativelayout>



You can do things like:
android:layout_below="@id/textView1"
  android:layout_toRightOf="@id/thatsId"

It makes life much easier. In the GUI editor, it looks like this...


No comments:

Post a Comment