Class RelativeLayout

All Implemented Interfaces:
Drawable.Callback, ViewManager, ViewParent
Direct Known Subclasses:
RelativeRadioGroup

public class RelativeLayout extends ViewGroup
A Layout where the positions of the children can be described in relation to each other or to the parent.

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

  • Field Details

    • TRUE

      public static final int TRUE
      See Also:
    • LEFT_OF

      public static final int LEFT_OF
      Rule that aligns a child's right edge with another child's left edge.
      See Also:
    • RIGHT_OF

      public static final int RIGHT_OF
      Rule that aligns a child's left edge with another child's right edge.
      See Also:
    • ABOVE

      public static final int ABOVE
      Rule that aligns a child's bottom edge with another child's top edge.
      See Also:
    • BELOW

      public static final int BELOW
      Rule that aligns a child's top edge with another child's bottom edge.
      See Also:
    • ALIGN_BASELINE

      public static final int ALIGN_BASELINE
      Rule that aligns a child's baseline with another child's baseline.
      See Also:
    • ALIGN_LEFT

      public static final int ALIGN_LEFT
      Rule that aligns a child's left edge with another child's left edge.
      See Also:
    • ALIGN_TOP

      public static final int ALIGN_TOP
      Rule that aligns a child's top edge with another child's top edge.
      See Also:
    • ALIGN_RIGHT

      public static final int ALIGN_RIGHT
      Rule that aligns a child's right edge with another child's right edge.
      See Also:
    • ALIGN_BOTTOM

      public static final int ALIGN_BOTTOM
      Rule that aligns a child's bottom edge with another child's bottom edge.
      See Also:
    • ALIGN_PARENT_LEFT

      public static final int ALIGN_PARENT_LEFT
      Rule that aligns the child's left edge with its RelativeLayout parent's left edge.
      See Also:
    • ALIGN_PARENT_TOP

      public static final int ALIGN_PARENT_TOP
      Rule that aligns the child's top edge with its RelativeLayout parent's top edge.
      See Also:
    • ALIGN_PARENT_RIGHT

      public static final int ALIGN_PARENT_RIGHT
      Rule that aligns the child's right edge with its RelativeLayout parent's right edge.
      See Also:
    • ALIGN_PARENT_BOTTOM

      public static final int ALIGN_PARENT_BOTTOM
      Rule that aligns the child's bottom edge with its RelativeLayout parent's bottom edge.
      See Also:
    • CENTER_IN_PARENT

      public static final int CENTER_IN_PARENT
      Rule that centers the child with respect to the bounds of its RelativeLayout parent.
      See Also:
    • CENTER_HORIZONTAL

      public static final int CENTER_HORIZONTAL
      Rule that centers the child horizontally with respect to the bounds of its RelativeLayout parent.
      See Also:
    • CENTER_VERTICAL

      public static final int CENTER_VERTICAL
      Rule that centers the child vertically with respect to the bounds of its RelativeLayout parent.
      See Also:
    • START_OF

      public static final int START_OF
      Rule that aligns a child's end edge with another child's start edge.
      See Also:
    • END_OF

      public static final int END_OF
      Rule that aligns a child's start edge with another child's end edge.
      See Also:
    • ALIGN_START

      public static final int ALIGN_START
      Rule that aligns a child's start edge with another child's start edge.
      See Also:
    • ALIGN_END

      public static final int ALIGN_END
      Rule that aligns a child's end edge with another child's end edge.
      See Also:
    • ALIGN_PARENT_START

      public static final int ALIGN_PARENT_START
      Rule that aligns the child's start edge with its RelativeLayout parent's start edge.
      See Also:
    • ALIGN_PARENT_END

      public static final int ALIGN_PARENT_END
      Rule that aligns the child's end edge with its RelativeLayout parent's end edge.
      See Also:
  • Constructor Details

    • RelativeLayout

      public RelativeLayout(Context context)
  • Method Details

    • shouldDelayChildPressedState

      public boolean shouldDelayChildPressedState()
      Description copied from class: ViewGroup
      Return true if the pressed state should be delayed for children or descendants of this ViewGroup. Generally, this should be done for containers that can scroll, such as a List. This prevents the pressed state from appearing when the user is actually trying to scroll the content.

      The default implementation returns true for compatibility reasons. Subclasses that do not scroll should generally override this method and return false.

      Overrides:
      shouldDelayChildPressedState in class ViewGroup
    • setIgnoreGravity

      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.START | Gravity.TOP.
      Parameters:
      viewId - The id of the View to be ignored by gravity, or 0 if no View should be ignored.
      See Also:
    • getIgnoreGravity

      public int getIgnoreGravity()
      Get the id of the View to be ignored by gravity
    • getGravity

      public int getGravity()
      Describes how the child views are positioned.
      Returns:
      the gravity.
      See Also:
    • setGravity

      public void setGravity(int gravity)
      Describes how the child views are positioned. Defaults to Gravity.START | Gravity.TOP.

      Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.

      Parameters:
      gravity - See Gravity
      See Also:
    • setHorizontalGravity

      public void setHorizontalGravity(int horizontalGravity)
    • setVerticalGravity

      public void setVerticalGravity(int verticalGravity)
    • getBaseline

      public int getBaseline()
      Description copied from class: View

      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.

      Overrides:
      getBaseline in class View
      Returns:
      the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported
    • requestLayout

      public void requestLayout()
      Description copied from class: View
      Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. This should not be called while the view hierarchy is currently in a layout pass (View.isInLayout(). If layout is happening, the request may be honored at the end of the current layout pass (and then layout will run again) or after the current frame is drawn and the next layout occurs.

      Subclasses which override this method should call the superclass method to handle possible request-during-layout errors correctly.

      Specified by:
      requestLayout in interface ViewParent
      Overrides:
      requestLayout in class View
    • onMeasure

      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      Description copied from class: View
      Measure the view and its content to determine the measured width and the measured height. This method is invoked by View.measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

      CONTRACT: When overriding this method, you must call View.setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by View.measure(int, int). Calling super.onMeasure() 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 the base one to provide better measurements of their content.

      Overrides:
      onMeasure in class View
      Parameters:
      widthMeasureSpec - width measure specification imposed by the parent MeasureSpec
      heightMeasureSpec - height measure specification imposed by the parent MeasureSpec
    • onLayout

      protected void onLayout(boolean changed, int left, int top, int right, int bottom)
      Description copied from class: View
      Called from View.layout(int, int, int, int) 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 children.

      Specified by:
      onLayout in class ViewGroup
      Parameters:
      changed - This is a new size or position for this view
      left - Left position, relative to parent
      top - Top position, relative to parent
      right - Right position, relative to parent
      bottom - Bottom position, relative to parent
    • generateLayoutParams

      @Nonnull protected ViewGroup.LayoutParams generateLayoutParams(@Nonnull ViewGroup.LayoutParams lp)
      Description copied from class: ViewGroup
      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 ViewGroup.checkLayoutParams(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.
      Overrides:
      generateLayoutParams in class ViewGroup
      Parameters:
      lp - The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
      Returns:
      an instance of ViewGroup.LayoutParams or one of its descendants
    • generateDefaultLayoutParams

      @Nonnull protected ViewGroup.LayoutParams generateDefaultLayoutParams()
      Description copied from class: ViewGroup
      Returns a set of default layout parameters. These parameters are requested when the View passed to ViewGroup.addView(View) has no layout parameters already set. If null is returned, an exception is thrown from addView.
      Overrides:
      generateDefaultLayoutParams in class ViewGroup
      Returns:
      a set of default layout parameters
    • checkLayoutParams

      protected boolean checkLayoutParams(@Nullable ViewGroup.LayoutParams p)
      Description copied from class: ViewGroup
      Check whether given params fit to this view group.

      See also ViewGroup.generateLayoutParams(LayoutParams) See also ViewGroup.generateDefaultLayoutParams()

      Overrides:
      checkLayoutParams in class ViewGroup
      Parameters:
      p - layout params to check
      Returns:
      if params matched to this view group