This example and the preceding LinearLayout Android ApiDemos example resolution (145): Views-> Layouts-> LinearLayout-> 5. simple Form implements the same function to construct an input Form, but this example uses the RelativeLayout layout. During actual development, you can decide which layout to use.
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/blue"
Android: padding = "10dip">
<! -
TextView goes at the top left by default.
->
<TextView
Android: id = "@ + id/label"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/relative_layout_2_instructions"/>
<! -
Put the EditText field under the TextView
Also give it a standard background (the "android :"
Part in @ android: drawable/editbox_background
Means it is system resource rather
An application resource.
->
<EditText
Android: id = "@ + id/entry"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ android: drawable/editbox_background"
Android: layout_below = "@ id/label"/>
<! -
The OK button goes below the EditText field.
It is also aligned to the right edge of the parent
(Respecting the parent's padding ).
The OK button comes first so the Cancel button
Can be specified relative to the OK button.
->
<Button
Android: id = "@ + id/OK"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/entry"
Android: layout_alignParentRight = "true"
Android: layout_marginLeft = "10dip"
Android: text = "@ string/relative_layout_2_ OK"/>
<! -
The Cancel button is aligned with the top
The OK button and positioned to the left of it.
Since the OK button has a left margin of 10, there
Is some space between the two buttons.
->
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_toLeftOf = "@ id/OK"
Android: layout_alignTop = "@ id/OK"
Android: text = "@ string/relative_layout_2_cancel"/>
</RelativeLayout>
RelativeLayout supports the following layout attributes:
Layout_above: place the bottom edge of the View on the top of the specified view.
Layout_alignBaseline: Align the baseline of the current View with the Baseline of the specified View.
Layout_alignBottom: Align the bottom edge of the current View with the bottom of the specified View.
Layout_alignLeft: align the current View with the left of the specified View.
Layout_alignParentBottom: align the current View with the bottom of the parent container.
Layout_alignParentLeft: align the current View with the left side of the parent container.
Layout_alignParentRight: align the current View with the right of the parent container.
Layout_alignParentTop: align the current View with the top of the parent container.
Layout_alignRight: align the current View with the right of the specified View.
Layout_alignTop: align the current View with the top of the specified View.
Layout_alignWithParentIfMissing: If the toLeftOf or toRightOf specified by the View cannot be found, the parent container is used as a substitute.
Layout_below: place the current View under the specified View.
Layout_centerHorzontal: if it is true, the View is horizontally centered in the parent container.
Layout_centerInParent: If this parameter is set to true, the View is centered both horizontally and vertically in the parent container.
Layout_centerVertical: if it is true, the View is vertically centered in the parent container.
Layout_toLeftOf: place the View on the left of the specified View
Layout_toRightOf: place the View on the right of the specified View.