150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpage.title=Relative Layout
250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainparent.title=Layouts
350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainparent.link=layout-objects.html
450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main@jd:body
550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div id="qv-wrapper">
750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div id="qv">
850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2>In this document</h2>
950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <ol>
1050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <li><a href="#Position">Positioning Views</a></li>
1150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <li><a href="#Example">Example</a></li>
1250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  </ol>
1350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <h2>Key classes</h2>
1450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <ol>
1550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>{@link android.widget.RelativeLayout}</li>
1650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>{@link android.widget.RelativeLayout.LayoutParams}</li>
1750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  </ol>
1850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
1950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
2050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
2150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>{@link android.widget.RelativeLayout} is a view group that displays child views in relative
2250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpositions. The position of each view can be specified as relative to sibling elements (such as to
2350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainthe left-of or below another view) or in positions relative to the parent {@link
2450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.RelativeLayout} area (such as aligned to the bottom, left of center).</p>
2550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
2650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/relativelayout.png" alt="" />
2750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
2850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>A {@link android.widget.RelativeLayout} is a very powerful utility for designing a user interface
2950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainbecause it can eliminate nested view groups and keep your layout hierarchy flat, which improves
3050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainperformance. If you find yourself using several nested {@link android.widget.LinearLayout} groups,
3150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainyou may be able to replace them with a single {@link android.widget.RelativeLayout}.</p>
3250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
3350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
3450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="Position">Positioning Views</h2>
3550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
3650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>{@link android.widget.RelativeLayout} lets child views specify their position relative to the
3750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainparent view or to each other (specified by ID). So you can align two elements by right border, or
3850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainmake one below another, centered in the screen, centered left, and so on. By default, all child
3950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainviews are drawn at the top-left of the layout, so you must define the position of each view
4050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainusing the various layout properties available from {@link
4150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.RelativeLayout.LayoutParams}.</p>
4250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 
4350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Some of the many layout properties available to views in a {@link android.widget.RelativeLayout}
4450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maininclude:</p>
4550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<dl>
4650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <dt><a
47f284d49293aead609de5b83d601260cfd86b7978Scott Mainhref="{@docRoot}reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_alignParentTop"
4850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main>{@code android:layout_alignParentTop}</a></dt>
4950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <dd>If {@code "true"}, makes the top edge of this view match the top edge of the parent. </dd>
5050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <dt><a
51f284d49293aead609de5b83d601260cfd86b7978Scott Mainhref="{@docRoot}reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_centerVertical"
5250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main>{@code android:layout_centerVertical}</a></dt>
5350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <dd>If {@code "true"}, centers this child vertically within its parent.</dd>
5450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <dt><a
55f284d49293aead609de5b83d601260cfd86b7978Scott Mainhref="{@docRoot}reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_below"
5650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main>{@code android:layout_below}</a></dt>
5750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <dd>Positions the top edge of this view below the view specified with a resource ID.</dd>
5850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <dt><a
59f284d49293aead609de5b83d601260cfd86b7978Scott Mainhref="{@docRoot}reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_toRightOf"
6050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main>{@code android:layout_toRightOf}</a></dt>
6150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <dd>Positions the left edge of this view to the right of the view specified with a resource ID.</dd>
6250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</dl>
6350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
6450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>These are just a few examples. All layout attributes are documented at {@link
6550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.RelativeLayout.LayoutParams}.</p>
6650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
6750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>The value for each layout property is either a boolean to
6850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainenable a layout position relative to the parent {@link android.widget.RelativeLayout} or an ID that
6950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainreferences another view in the layout against which the view should be positioned.</p>
7050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
7150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>In your XML layout, dependencies against other views in the layout can be declared in any order.
7250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainFor example, you can declare that "view1" be positioned below "view2" even if "view2" is the last
7350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainview declared in the hierarchy. The example below demonstrates such a scenario.</p>
7450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
7550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
7650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="Example">Example</h2>
7750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
7850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Each of the attributes that control the relative position of each view are emphasized.</p>
7950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="figure" style="width:220px;margin-top:0">
8050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/sample-relativelayout.png" alt="" />
8150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
8250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
8350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
8450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;?xml version="1.0" encoding="utf-8"?>
8550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
8650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_width="fill_parent"
8750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_height="fill_parent"
8850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:paddingLeft="16dp"
8950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:paddingRight="16dp" >
9050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    &lt;EditText
9150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:id="@+id/name"
9250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_width="fill_parent"
9350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_height="wrap_content"
9450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:hint="@string/reminder" />
9550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    &lt;Spinner
9650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:id="@+id/dates"
9750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_width="0dp"
9850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_height="wrap_content"
9950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        <strong>android:layout_below="@id/name"</strong>
10050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        <strong>android:layout_alignParentLeft="true"</strong>
10150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        <strong>android:layout_toLeftOf="@+id/times"</strong> />
10250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    &lt;Spinner
10350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:id="@id/times"
10450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_width="96dp"
10550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_height="wrap_content"
10650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        <strong>android:layout_below="@id/name"</strong>
10750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        <strong>android:layout_alignParentRight="true"</strong> />
10850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    &lt;Button
10950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_width="96dp"
11050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:layout_height="wrap_content"
11150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        <strong>android:layout_below="@id/times"</strong>
11250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        <strong>android:layout_alignParentRight="true"</strong>
11350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        android:text="@string/done" />
11450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;/RelativeLayout>
11550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
11650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
11750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>For details about all the layout attributes available to each child view of a {@link
11850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.RelativeLayout}, see {@link android.widget.RelativeLayout.LayoutParams}.</p>