: Beginning Android

Concepts and Properties

Concepts and Properties

To make all this work, we need ways to reference other widgets within an XML layout file, plus ways to indicate the relative positions of those widgets.

Positions Relative to a Container

The easiest relations to set up tie a widgets position to that of its container:

android:layout_alignParentTop says the widgets top should align with the top of the container.

android:layout_alignParentBottom says the widgets bottom should align with the bottom of the container.

android:layout_alignParentLeft says the widgets left side should align with the left side of the container.

android:layout_alignParentRight says the widgets right side should align with the right side of the container.

android:layout_centerHorizontal says the widget should be positioned horizontally at the center of the container.

android:layout_centerVertical says the widget should be positioned vertically at the center of the container.

android:layout_centerInParent says the widget should be positioned both horizontally and vertically at the center of the container.

All of these properties take a simple Boolean value (true or false).

Note that the padding of the widget is taken into account when performing these various alignments. The alignments are based on the widgets overall cell (a combination of its natural space plus the padding).

Relative Notation in Properties

The remaining properties of relevance to RelativeLayout take as a value the identity of a widget in the container. To identify and reference widgets this way, follow these steps:

1.Put identifiers (android:id attributes) on all elements that you will need to address, of the form @+id/....

2.Reference other widgets using the same identifier value without the plus sign (@id/...).

For example, if Widget A is identified as @+id/widget_a, Widget B can refer to Widget A in one of its own properties via the identifier @id/widget_a.

Positions Relative to Other Widgets

There are four properties that control position of a widget in relation to other widgets:

android:layout_above indicates that the widget should be placed above the widget referenced in the property.

android:layout_below indicates that the widget should be placed below the widget referenced in the property.

android:layout_toLeftOf indicates that the widget should be placed to the left of the widget referenced in the property.

android:layout_toRightOf indicates that the widget should be placed to the right of the widget referenced in the property.

There are five additional properties that can control one widgets alignment relative to another:

android:layout_alignTop indicates that the widgets top should be aligned with the top of the widget referenced in the property.

android:layout_alignBottom indicates that the widgets bottom should be aligned with the bottom of the widget referenced in the property.

android:layout_alignLeft indicates that the widgets left side should be aligned with the left side of the widget referenced in the property.

android:layout_alignRight indicates that the widgets right side should be aligned with the right side of the widget referenced in the property.

android:layout_alignBaseline indicates that the baselines of the two widgets should be aligned.

The last property in the list is useful for aligning labels and fields so that the text appears natural. Since fields have a box around them and labels do not, android:layout_alignTop will align the top of the fields box with the top of the label, which will cause the text of the label to be higher on-screen than the text entered into the field.

So, if we want Widget B to be positioned to the right of Widget A, in the XML element for Widget B we need to include android:layout_toRight="@id/widget_a" (assuming @id/widget_a is the identity of Widget A).

Order of Evaluation

What makes this even more complicated is the order of evaluation. Android makes a single pass through your XML layout and computes the size and position of each widget in sequence. This has a couple of ramifications:

You cannot reference a widget that has not yet been defined in the file.

You must be careful that any uses of fill_parent in android:layout_width or android:layout_height do not eat up all the space before subsequent widgets have been defined.


: 0.045. /Cache: 0 / 0