Relative Layout

1938 views.

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a singleRelativeLayout.

Positioning Views

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.
Some of the many layout properties available to views in a RelativeLayout include:

android:layout_alignParentTop
If “true”, makes the top edge of this view match the top edge of the parent.
android:layout_centerVertical
If “true”, centers this child vertically within its parent.
android:layout_below
Positions the top edge of this view below the view specified with a resource ID.
android:layout_toRightOf
Positions the left edge of this view to the right of the view specified with a resource ID.

The value for each layout property is either a boolean to enable a layout position relative to the parent RelativeLayout or an ID that references another view in the layout against which the view should be positioned.
In your XML layout, dependencies against other views in the layout can be declared in any order. For example, you can declare that “view1” be positioned below “view2” even if “view2” is the last view declared in the hierarchy. The example below demonstrates such a scenario.

Example

Each of the attributes that control the relative position of each view are emphasized.

rl1

RelativeLayout.LayoutParams

XML attributes

android:layout_above
Positions the bottom edge of this view above the given anchor view ID. Accommodates bottom margin of this view and top margin of anchor view.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_above.

android:layout_alignBaseline
Positions the baseline of this view on the baseline of the given anchor view ID.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_alignBaseline.

android:layout_alignBottom
Makes the bottom edge of this view match the bottom edge of the given anchor view ID. Accommodates bottom margin.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_alignBottom.

android:layout_alignEnd
Makes the end edge of this view match the end edge of the given anchor view ID. Accommodates end margin.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_alignEnd.

android:layout_alignLeft
Makes the left edge of this view match the left edge of the given anchor view ID. Accommodates left margin.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_alignLeft.

android:layout_alignParentBottom
If true, makes the bottom edge of this view match the bottom edge of the parent. Accommodates bottom margin.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_alignParentBottom.

android:layout_alignParentEnd
If true, makes the end edge of this view match the end edge of the parent. Accommodates end margin.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_alignParentEnd.

android:layout_alignParentLeft
If true, makes the left edge of this view match the left edge of the parent. Accommodates left margin.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_alignParentLeft

android:layout_alignParentRight
If true, makes the right edge of this view match the right edge of the parent. Accommodates right margin.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_alignParentRight.

android:layout_alignParentStart
If true, makes the start edge of this view match the start edge of the parent. Accommodates start margin.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_alignParentStart.

android:layout_alignParentTop
If true, makes the top edge of this view match the top edge of the parent. Accommodates top margin.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_alignParentTop.

android:layout_alignRight
Makes the right edge of this view match the right edge of the given anchor view ID. Accommodates right margin.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_alignRight

android:layout_alignStart
Makes the start edge of this view match the start edge of the given anchor view ID. Accommodates start margin.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_alignStart.

android:layout_alignTop
Makes the top edge of this view match the top edge of the given anchor view ID. Accommodates top margin.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_alignTop.

android:layout_alignWithParentIfMissing
If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_alignWithParentIfMissing.

android:layout_below
Positions the top edge of this view below the given anchor view ID. Accommodates top margin of this view and bottom margin of anchor view.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_below.

android:layout_centerHorizontal
If true, centers this child horizontally within its parent.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_centerHorizontal.

android:layout_centerInParent
If true, centers this child horizontally and vertically within its parent.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_centerInParent.

android:layout_centerVertical
If true, centers this child vertically within its parent.
Must be a boolean value, either “true” or “false”.
This may also be a reference to a resource (in the form “@[package:]type:name”) or theme attribute (in the form “?[package:][type:]name”) containing a value of this type.
This corresponds to the global attribute resource symbol layout_centerVertical.

android:layout_toEndOf
Positions the start edge of this view to the end of the given anchor view ID. Accommodates start margin of this view and end margin of anchor view.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_toEndOf.

android:layout_toLeftOf
Positions the right edge of this view to the left of the given anchor view ID. Accommodates right margin of this view and left margin of anchor view.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_toLeftOf.

android:layout_toRightOf
Positions the left edge of this view to the right of the given anchor view ID. Accommodates left margin of this view and right margin of anchor view.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_toRightOf.

android:layout_toStartOf

Positions the end edge of this view to the start of the given anchor view ID. Accommodates end margin of this view and start margin of anchor view.
Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
This corresponds to the global attribute resource symbol layout_toStartOf.

You may also like...

Leave a Reply