Android
android.widget
public class

android.widget.RelativeLayout

java.lang.Object
android.view.View Drawable.Callback KeyEvent.Callback
android.view.ViewGroup ViewManager ViewParent
android.widget.RelativeLayout

A Layout where the positions of the children can be described in relation to each other or to the parent. For the sake of efficiency, the relations between views are evaluated in one pass, so if view Y is dependent on the position of view X, make sure the view X comes first in the layout.

Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

Also see RelativeLayout.LayoutParams for layout attributes

Nested Classes
RelativeLayout.LayoutParams Per-child layout information associated with RelativeLayout. 
Known Direct Subclasses

Summary

XML Attributes

Attribute name Related methods  
android:gravity setGravity(int)
 
Specifies how to place an object, both its x and y axis, within a larger containing object. 
android:ignoreGravity setIgnoreGravity(int)
 
Indicates what view should not be affected by gravity. 
XML Attributes inherited from class android.view.ViewGroup
XML Attributes inherited from class android.view.View

Constants

      Value  
int  ABOVE  Rule that aligns a child's bottom edge with another child's top edge.  0x00000002 
int  ALIGN_BASELINE  Rule that aligns a child's baseline with another child's baseline.  0x00000004 
int  ALIGN_BOTTOM  Rule that aligns a child's bottom edge with another child's bottom edge.  0x00000008 
int  ALIGN_LEFT  Rule that aligns a child's left edge with another child's left edge.  0x00000005 
int  ALIGN_PARENT_BOTTOM  Rule that aligns the child's bottom edge with its RelativeLayout parent's bottom edge.  12  0x0000000c 
int  ALIGN_PARENT_LEFT  Rule that aligns the child's left edge with its RelativeLayout parent's left edge.  0x00000009 
int  ALIGN_PARENT_RIGHT  Rule that aligns the child's right edge with its RelativeLayout parent's right edge.  11  0x0000000b 
int  ALIGN_PARENT_TOP  Rule that aligns the child's top edge with its RelativeLayout parent's top edge.  10  0x0000000a 
int  ALIGN_RIGHT  Rule that aligns a child's right edge with another child's right edge.  0x00000007 
int  ALIGN_TOP  Rule that aligns a child's top edge with another child's top edge.  0x00000006 
int  BELOW  Rule that aligns a child's top edge with another child's bottom edge.  0x00000003 
int  CENTER_HORIZONTAL  Rule that centers the child horizontally with respect to the bounds of its RelativeLayout parent.  14  0x0000000e 
int  CENTER_IN_PARENT  Rule that centers the child with respect to the bounds of its RelativeLayout parent.  13  0x0000000d 
int  CENTER_VERTICAL  Rule that centers the child vertically with respect to the bounds of its RelativeLayout parent.  15  0x0000000f 
int  LEFT_OF  Rule that aligns a child's right edge with another child's left edge.  0x00000000 
int  RIGHT_OF  Rule that aligns a child's left edge with another child's right edge.  0x00000001 
int  TRUE    -1  0xffffffff 
Constants inherited from class android.view.ViewGroup
Constants inherited from class android.view.View
Fields inherited from class android.view.ViewGroup
Fields inherited from class android.view.View

Public Constructors

            RelativeLayout(Context context)
            RelativeLayout(Context context, AttributeSet attrs)
            RelativeLayout(Context context, AttributeSet attrs, int defStyle)

Public Methods

          RelativeLayout.LayoutParams  generateLayoutParams(AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
          int  getBaseline()

Return the offset of the widget's text baseline from the widget's top boundary.

          void  setGravity(int gravity)
Describes how the child views are positioned.
          void  setHorizontalGravity(int horizontalGravity)
          void  setIgnoreGravity(int viewId)
Defines which View is ignored when the gravity is applied.
          void  setVerticalGravity(int verticalGravity)

Protected Methods

          boolean  checkLayoutParams(ViewGroup.LayoutParams p)
          ViewGroup.LayoutParams  generateDefaultLayoutParams()
Returns a set of layout parameters with a width of WRAP_CONTENT, a height of WRAP_CONTENT and no spanning.
          ViewGroup.LayoutParams  generateLayoutParams(ViewGroup.LayoutParams p)
Returns a safe set of layout parameters based on the supplied layout params.
          void  onLayout(boolean changed, int l, int t, int r, int b)
Called from layout when this view should assign a size and position to each of its children.
          void  onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

Methods inherited from class android.view.ViewGroup
Methods inherited from class android.view.View
Methods inherited from class java.lang.Object
Methods inherited from interface android.graphics.drawable.Drawable.Callback
Methods inherited from interface android.view.KeyEvent.Callback
Methods inherited from interface android.view.ViewManager
Methods inherited from interface android.view.ViewParent

Details

XML Attributes

android:gravity

Specifies how to place an object, both its x and y axis, within a larger containing object.

Must be one or more (separated by '|') of the following constant values.

ConstantValueDescription
top0x30 Push object to the top of its container, not changing its size.
bottom0x50 Push object to the bottom of its container, not changing its size.
left0x03 Push object to the left of its container, not changing its size.
right0x05 Push object to the right of its container, not changing its size.
center_vertical0x10 Place object in the vertical center of its container, not changing its size.
fill_vertical0x70 Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal0x01 Place object in the horizontal center of its container, not changing its size.
fill_horizontal0x07 Grow the horizontal size of the object if needed so it completely fills its container.
center0x11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill0x77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.

This corresponds to the global attribute resource symbol gravity.

Related Methods

android:ignoreGravity

Indicates what view should not be affected by gravity.

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 ignoreGravity.

Related Methods

Constants

public static final int ABOVE

Rule that aligns a child's bottom edge with another child's top edge.
Constant Value: 2 (0x00000002)

public static final int ALIGN_BASELINE

Rule that aligns a child's baseline with another child's baseline.
Constant Value: 4 (0x00000004)

public static final int ALIGN_BOTTOM

Rule that aligns a child's bottom edge with another child's bottom edge.
Constant Value: 8 (0x00000008)

public static final int ALIGN_LEFT

Rule that aligns a child's left edge with another child's left edge.
Constant Value: 5 (0x00000005)

public static final int ALIGN_PARENT_BOTTOM

Rule that aligns the child's bottom edge with its RelativeLayout parent's bottom edge.
Constant Value: 12 (0x0000000c)

public static final int ALIGN_PARENT_LEFT

Rule that aligns the child's left edge with its RelativeLayout parent's left edge.
Constant Value: 9 (0x00000009)

public static final int ALIGN_PARENT_RIGHT

Rule that aligns the child's right edge with its RelativeLayout parent's right edge.
Constant Value: 11 (0x0000000b)

public static final int ALIGN_PARENT_TOP

Rule that aligns the child's top edge with its RelativeLayout parent's top edge.
Constant Value: 10 (0x0000000a)

public static final int ALIGN_RIGHT

Rule that aligns a child's right edge with another child's right edge.
Constant Value: 7 (0x00000007)

public static final int ALIGN_TOP

Rule that aligns a child's top edge with another child's top edge.
Constant Value: 6 (0x00000006)

public static final int BELOW

Rule that aligns a child's top edge with another child's bottom edge.
Constant Value: 3 (0x00000003)

public static final int CENTER_HORIZONTAL

Rule that centers the child horizontally with respect to the bounds of its RelativeLayout parent.
Constant Value: 14 (0x0000000e)

public static final int CENTER_IN_PARENT

Rule that centers the child with respect to the bounds of its RelativeLayout parent.
Constant Value: 13 (0x0000000d)

public static final int CENTER_VERTICAL

Rule that centers the child vertically with respect to the bounds of its RelativeLayout parent.
Constant Value: 15 (0x0000000f)

public static final int LEFT_OF

Rule that aligns a child's right edge with another child's left edge.
Constant Value: 0 (0x00000000)

public static final int RIGHT_OF

Rule that aligns a child's left edge with another child's right edge.
Constant Value: 1 (0x00000001)

public static final int TRUE

Constant Value: -1 (0xffffffff)

Public Constructors

public RelativeLayout(Context context)

public RelativeLayout(Context context, AttributeSet attrs)

public RelativeLayout(Context context, AttributeSet attrs, int defStyle)

Public Methods

public RelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs)

Returns a new set of layout parameters based on the supplied attributes set.

Parameters

attrs the attributes to build the layout parameters from

Returns

public int getBaseline()

Return the offset of the widget's text baseline from the widget's top boundary. If this widget does not support baseline alignment, this method returns -1.

Returns

  • the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported

public void setGravity(int gravity)

Describes how the child views are positioned. Defaults to Gravity.LEFT | Gravity.TOP.

Related XML Attributes

Parameters

gravity See Gravity

public void setHorizontalGravity(int horizontalGravity)

public void setIgnoreGravity(int viewId)

Defines which View is ignored when the gravity is applied. This setting has no effect if the gravity is Gravity.LEFT | Gravity.TOP.

Related XML Attributes

Parameters

viewId The id of the View to be ignored by gravity, or 0 if no View should be ignored.

See Also

public void setVerticalGravity(int verticalGravity)

Protected Methods

protected boolean checkLayoutParams(ViewGroup.LayoutParams p)

protected ViewGroup.LayoutParams generateDefaultLayoutParams()

Returns a set of layout parameters with a width of WRAP_CONTENT, a height of WRAP_CONTENT and no spanning.

Returns

  • a set of default layout parameters or null

protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)

Returns a safe set of layout parameters based on the supplied layout params. When a ViewGroup is passed a View whose layout params do not pass the test of checkLayoutParams(android.view.ViewGroup.LayoutParams), this method is invoked. This method should return a new set of layout params suitable for this ViewGroup, possibly by copying the appropriate attributes from the specified set of layout params.

Parameters

p The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.

Returns

protected void onLayout(boolean changed, int l, int t, int r, int b)

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their their children.

Parameters

changed This is a new size or position for this view
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters

widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:48