- Home Startseite ホーム 主页 首頁
- SDK
- Dev Guide Handbuch Guía Guide Guida 開発ガイド 开发人员指南 開發指南
- Reference Referenz Referencia Référence Riferimento リファレンス 参考 參考資料
- Resources
- Videos ビデオ
- Blog ブログ 博客 網誌

Android.com

Hello, Views >

# Hello, RelativeLayout

A `RelativeLayout` is a ViewGroup that allows you to layout child elements in positions relative to the parent or siblings elements.

1. Start a new project/Activity called HelloRelativeLayout.
2. Open the layout file. Make it like so:

       <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent">

           <TextView
               android:id="@+id/label"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:text="Type here:"/>

           <EditText
               android:id="@+id/entry"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:background="@android:drawable/editbox_background"
               android:layout_below="@id/label"/>

           <Button
               android:id="@+id/ok"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_below="@id/entry"
               android:layout_alignParentRight="true"
               android:layout_marginLeft="10dip"
               android:text="OK" />

           <Button
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_toLeftOf="@id/ok"
               android:layout_alignTop="@id/ok"
               android:text="Cancel" />

       </RelativeLayout>

   Pay attention to each of the additional `layout_*` attributes (besides the usual width and height, which are required for all elements). When using relative layout, we use attributes like `layout_below` and `layout_toLeftOf` to describe how we'd like to position each View. Naturally, these are different relative positions, and the value of the attribute is the id of the element we want the position relative to.

3. Make sure your Activity loads this layout in the `onCreate()` method:

       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
       }

   `R.layout.main` refers to the `main.xml` layout file.

4. Run it.

You should see the following:

### Resources

- `RelativeLayout`
- `TextView`
- `EditText`
- `Button`

↑ Go to top

← Back to Hello, Views