Skip to content

Most visited

Recently visited

navigation

RelativeLayout.LayoutParams

public static class RelativeLayout.LayoutParams
extends ViewGroup.MarginLayoutParams

java.lang.Object
   ↳ android.view.ViewGroup.LayoutParams
     ↳ android.view.ViewGroup.MarginLayoutParams
       ↳ android.widget.RelativeLayout.LayoutParams


Specifies how a view is positioned within a RelativeLayout. The relative layout containing the view uses the value of these layout parameters to determine where to position the view on the screen. If the view is not contained within a relative layout, these attributes are ignored. See the Relative Layout guide for example code demonstrating how to use relative layout’s layout parameters in a layout XML. To learn more about layout parameters and how they differ from typical view attributes, see the Layouts guide.

Summary

XML attributes

android:layout_above Positions the bottom edge of this view above the given anchor view ID. 
android:layout_alignBaseline Positions the baseline of this view on the baseline of the given anchor view ID. 
android:layout_alignBottom Makes the bottom edge of this view match the bottom edge of the given anchor view ID. 
android:layout_alignEnd Makes the end edge of this view match the end edge of the given anchor view ID. 
android:layout_alignLeft Makes the left edge of this view match the left edge of the given anchor view ID. 
android:layout_alignParentBottom If true, makes the bottom edge of this view match the bottom edge of the parent. 
android:layout_alignParentEnd If true, makes the end edge of this view match the end edge of the parent. 
android:layout_alignParentLeft If true, makes the left edge of this view match the left edge of the parent. 
android:layout_alignParentRight If true, makes the right edge of this view match the right edge of the parent. 
android:layout_alignParentStart If true, makes the start edge of this view match the start edge of the parent. 
android:layout_alignParentTop If true, makes the top edge of this view match the top edge of the parent. 
android:layout_alignRight Makes the right edge of this view match the right edge of the given anchor view ID. 
android:layout_alignStart Makes the start edge of this view match the start edge of the given anchor view ID. 
android:layout_alignTop Makes the top edge of this view match the top edge of the given anchor view ID. 
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. 
android:layout_below Positions the top edge of this view below the given anchor view ID. 
android:layout_centerHorizontal If true, centers this child horizontally within its parent. 
android:layout_centerInParent If true, centers this child horizontally and vertically within its parent. 
android:layout_centerVertical If true, centers this child vertically within its parent. 
android:layout_toEndOf Positions the start edge of this view to the end of the given anchor view ID. 
android:layout_toLeftOf Positions the right edge of this view to the left of the given anchor view ID. 
android:layout_toRightOf Positions the left edge of this view to the right of the given anchor view ID. 
android:layout_toStartOf Positions the end edge of this view to the start of the given anchor view ID. 

Inherited XML attributes

From class android.view.ViewGroup.MarginLayoutParams
From class android.view.ViewGroup.LayoutParams

Inherited constants

From class android.view.ViewGroup.LayoutParams

Fields

public boolean alignWithParent

When true, uses the parent as the anchor if the anchor doesn't exist or if the anchor's visibility is GONE.

Inherited fields

From class android.view.ViewGroup.MarginLayoutParams
From class android.view.ViewGroup.LayoutParams

Public constructors

RelativeLayout.LayoutParams(Context c, AttributeSet attrs)
RelativeLayout.LayoutParams(int w, int h)
RelativeLayout.LayoutParams(ViewGroup.LayoutParams source)

RelativeLayout.LayoutParams(ViewGroup.MarginLayoutParams source)

RelativeLayout.LayoutParams(RelativeLayout.LayoutParams source)

Copy constructor.

Public methods

void addRule(int verb, int subject)

Adds a layout rule to be interpreted by the RelativeLayout.

void addRule(int verb)

Adds a layout rule to be interpreted by the RelativeLayout.

String debug(String output)

Returns a String representation of this set of layout parameters.

int getRule(int verb)

Returns the layout rule associated with a specific verb.

int[] getRules()

Retrieves a complete list of all supported rules, where the index is the rule verb, and the element value is the value specified, or "false" if it was never set.

void removeRule(int verb)

Removes a layout rule to be interpreted by the RelativeLayout.

void resolveLayoutDirection(int layoutDirection)

This will be called by requestLayout() to resolve layout parameters that are relative to the layout direction.

Inherited methods

From class android.view.ViewGroup.MarginLayoutParams
From class android.view.ViewGroup.LayoutParams
From class java.lang.Object

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.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:layout_alignBaseline

Positions the baseline of this view on the baseline of the given anchor view ID.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:layout_alignBottom

Makes the bottom edge of this view match the bottom edge of the given anchor view ID. Accommodates bottom margin.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:layout_alignEnd

Makes the end edge of this view match the end edge of the given anchor view ID. Accommodates end margin.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:layout_alignLeft

Makes the left edge of this view match the left edge of the given anchor view ID. Accommodates left margin.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:layout_alignParentBottom

If true, makes the bottom edge of this view match the bottom edge of the parent. Accommodates bottom margin.

May be a boolean value, such as "true" or "false".

android:layout_alignParentEnd

If true, makes the end edge of this view match the end edge of the parent. Accommodates end margin.

May be a boolean value, such as "true" or "false".

android:layout_alignParentLeft

If true, makes the left edge of this view match the left edge of the parent. Accommodates left margin.

May be a boolean value, such as "true" or "false".

android:layout_alignParentRight

If true, makes the right edge of this view match the right edge of the parent. Accommodates right margin.

May be a boolean value, such as "true" or "false".

android:layout_alignParentStart

If true, makes the start edge of this view match the start edge of the parent. Accommodates start margin.

May be a boolean value, such as "true" or "false".

android:layout_alignParentTop

If true, makes the top edge of this view match the top edge of the parent. Accommodates top margin.

May be a boolean value, such as "true" or "false".

android:layout_alignRight

Makes the right edge of this view match the right edge of the given anchor view ID. Accommodates right margin.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:layout_alignStart

Makes the start edge of this view match the start edge of the given anchor view ID. Accommodates start margin.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:layout_alignTop

Makes the top edge of this view match the top edge of the given anchor view ID. Accommodates top margin.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

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.

May be a boolean value, such as "true" or "false".

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.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

android:layout_centerHorizontal

If true, centers this child horizontally within its parent.

May be a boolean value, such as "true" or "false".

android:layout_centerInParent

If true, centers this child horizontally and vertically within its parent.

May be a boolean value, such as "true" or "false".

android:layout_centerVertical

If true, centers this child vertically within its parent.

May be a boolean value, such as "true" or "false".

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.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

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.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

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.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

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.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Fields

alignWithParent

added in API level 1
boolean alignWithParent

When true, uses the parent as the anchor if the anchor doesn't exist or if the anchor's visibility is GONE.

Public constructors

RelativeLayout.LayoutParams

added in API level 1
RelativeLayout.LayoutParams (Context c, 
                AttributeSet attrs)

Parameters
c Context

attrs AttributeSet

RelativeLayout.LayoutParams

added in API level 1
RelativeLayout.LayoutParams (int w, 
                int h)

Parameters
w int

h int

RelativeLayout.LayoutParams

added in API level 1
RelativeLayout.LayoutParams (ViewGroup.LayoutParams source)

Parameters
source ViewGroup.LayoutParams

RelativeLayout.LayoutParams

added in API level 1
RelativeLayout.LayoutParams (ViewGroup.MarginLayoutParams source)

Parameters
source ViewGroup.MarginLayoutParams

RelativeLayout.LayoutParams

added in API level 19
RelativeLayout.LayoutParams (RelativeLayout.LayoutParams source)

Copy constructor. Clones the width, height, margin values, and rules of the source.

Parameters
source RelativeLayout.LayoutParams: The layout params to copy from.

Public methods

addRule

added in API level 1
void addRule (int verb, 
                int subject)

Adds a layout rule to be interpreted by the RelativeLayout.

Use this for verbs that refer to a sibling (ex. ALIGN_RIGHT) or take a boolean value (ex. CENTER_IN_PARENT).

If the rule is relative to the layout direction (ex. START_OF), then the layout direction must be resolved using resolveLayoutDirection(int) before calling getRule(int) with an absolute rule (ex. LEFT_OF.

Parameters
verb int: a layout verb, such as ALIGN_RIGHT

subject int: the ID of another view to use as an anchor, or a boolean value (represented as TRUE for true or 0 for false)

addRule

added in API level 1
void addRule (int verb)

Adds a layout rule to be interpreted by the RelativeLayout.

This method should only be used for verbs that don't refer to a sibling (ex. ALIGN_RIGHT) or take a boolean value (TRUE for true or 0 for false). To specify a verb that takes a subject, use addRule(int, int).

If the rule is relative to the layout direction (ex. ALIGN_PARENT_START), then the layout direction must be resolved using resolveLayoutDirection(int) before calling getRule(int) an absolute rule (ex. ALIGN_PARENT_LEFT.

Parameters
verb int: a layout verb, such as ALIGN_PARENT_LEFT

debug

added in API level 1
String debug (String output)

Returns a String representation of this set of layout parameters.

Parameters
output String: the String to prepend to the internal representation

Returns
String a String with the following format: output + "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }"

getRule

added in API level 23
int getRule (int verb)

Returns the layout rule associated with a specific verb.

Parameters
verb int: one of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT

Returns
int the id of another view to use as an anchor, a boolean value (represented as TRUE for true or 0 for false), or -1 for verbs that don't refer to another sibling (for example, ALIGN_WITH_PARENT_BOTTOM)

getRules

added in API level 1
int[] getRules ()

Retrieves a complete list of all supported rules, where the index is the rule verb, and the element value is the value specified, or "false" if it was never set. There will be no resolution of relative rules done.

Returns
int[] the supported rules

See also:

removeRule

added in API level 17
void removeRule (int verb)

Removes a layout rule to be interpreted by the RelativeLayout.

If the rule is relative to the layout direction (ex. START_OF, ALIGN_PARENT_START, etc.) then the layout direction must be resolved using resolveLayoutDirection(int) before before calling getRule(int) with an absolute rule (ex. LEFT_OF.

Parameters
verb int: One of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT.

resolveLayoutDirection

added in API level 17
void resolveLayoutDirection (int layoutDirection)

This will be called by requestLayout() to resolve layout parameters that are relative to the layout direction.

After this method is called, any rules using layout-relative verbs (ex. START_OF) previously added via addRule(int) may only be accessed via their resolved absolute verbs (ex. LEFT_OF).

Parameters
layoutDirection int: the direction of the layout LAYOUT_DIRECTION_LTR LAYOUT_DIRECTION_RTL

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)