Recipe Setting Relative Layout and Layout ID

Sometimes it is more convenient to set the layout relative to a starting object or parent object rather than absolute rules. Also, if the UI starts nesting LinearLayouts, it might be simpler to use relative layouts.This can be done using a RelativeLayout view, as shown in Listing 4.3.The layout is shown in Figure 4.7.

high left middle low

Figure 4.7 Four text views from the RelativeLayout example.

Listing 4.3 RelativeLayout example

<?xml version="1.0" encoding="utf-8"?>

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

<TextView android:id="@+id/mid" android:text="middle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/> <TextView android:id="@+id/high" android:text="high" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/mid"/> <TextView android:id="@+id/low" android:text="low" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/mid"/> <TextView android:id="@+id/left" android:text="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/high" android:layout_toLeftOf="@id/low" />

</RelativeLayout>

The explanation of these attributes and a list of the different available rules for relative layout are collected in Table 4.2. Because every layout can have portions declared in XML files and other portions in Java code, both methods of referring to layouts are shown.The first three rows of the table show attributes that need to point to a view ID, and the last two rows show attributes that are boolean.

Table 4.2 Possible Rules for Children in a Relative Layout

XML Attribute (All Start with

Relative Layout Rule

the android: Tag)

Java Constant

Align this view's edge

layout_above

ABOVE

relative to anchor view's

layout below

BELOW

edge

layout_toRightOf

RIGHT_OF

layout_toLeftOf

LEFT_OF

Align this view's edge

layout_alignTop

ALIGN_TOP

with anchor view's edge

layout_alignBottom

ALIGN_BOTTOM

layout_alignRight

ALIGN_RIGHT

layout_alignLeft

ALIGN_LEFT

Align this view's text

layout alignBaseline

ALIGN_BASELINE

baseline with anchor

view's text baseline

Align this view's edge

layout_alignParentTop

ALIGN_PARENT_TOP

with parent view's edge

layout_alignParentBottom

ALIGN_PARENT_BOTTOM

layout_alignParentRight

ALIGN_PARENT_RIGHT

layout_alignParentLeft

ALIGN_PARENT_LEFT

Center this view within

layout centerInParent

CENTER_IN_PARENT

parent

layout_centerHorizontal

CENTER_HORIZONTAL

layout_centerVertical

CENTER_VERTICAL

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment