Positioning Views in RelativeLayout
XML Attributes | Description |
---|---|
layout_alignParentTop | Accepts boolean value. If specified, the widget will be docked to the top of RelativeLayout. |
layout_alignParentBottom | Accepts boolean value. If specified, the widget will be docked to the bottom of RelativeLayout. |
layout_alignParentLeft | Accepts boolean value. If specified, the widget will be docked to the left edge of RelativeLayout. |
layout_alignParentRight | Accepts boolean value. If specified, the widget will be docked to the right edge of RelativeLayout. |
layout_centerInParent | Accepts boolean value. If specified, the widget will be aligned to center of RelativeLayout. |
layout_centerHorizontal | Accepts boolean value. If specified, the widget will be horizontally center aligned |
layout_centerVertical | Accepts boolean value. If specified, the widget will be vertically center aligned |
layout_below | Accepts sibling widget id. Places the widget below the view as specified widget id. |
layout_above | Accepts sibling widget id. Places the widget above the specified widget id. |
layout_toRightOf | Accepts sibling widget id. Places the widget to right of the view as specified widget id. |
layout_toLeftOf | Accepts sibling widget id. Places the widget to left of the view as specified widget id. |
layout_toEndOf | Accepts sibling widget id. Places the widget to end of the view as specified widget id. |
layout_toStartOf | Accepts sibling widget id. Places the widget to the beginning of the view as specified widget id. |
Some Example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:text="RED"
android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:background="#f00"
android:gravity="center"
android:textColor="#000"
android:layout_width="wrap_content"
android:padding="25dp"></TextView>
<TextView
android:text="ORANGE"
android:layout_height="wrap_content"
android:background="#ffa500"
android:gravity="center"
android:textColor="#000"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:padding="25dp"></TextView>
<TextView
android:text="YELLOW"
android:layout_height="wrap_content"
android:background="#ffff00"
android:gravity="center"
android:textColor="#000"
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:padding="25dp"></TextView>
<TextView
android:text="GREEN"
android:layout_height="wrap_content"
android:background="#0f0"
android:gravity="center"
android:textColor="#000"
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/TextView05"
android:padding="25dp"
android:layout_centerVertical="true"></TextView>
<TextView
android:text="BLUE"
android:layout_height="wrap_content"
android:background="#00f"
android:gravity="center"
android:textColor="#fff"
android:id="@+id/TextView05"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:padding="25dp"></TextView>
<TextView
android:text="INDIGO"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#fff"
android:id="@+id/TextView06"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/TextView05"
android:background="#4b0082"
android:padding="25dp"
android:layout_centerVertical="true"></TextView>
<TextView
android:text="VIOLET"
android:layout_height="wrap_content"
android:background="#ee82ee"
android:gravity="center"
android:textColor="#000"
android:id="@+id/TextView07"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:padding="25dp"></TextView>
</RelativeLayout>
Example 3:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/buttone1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="5dp"
android:text="Button1" />
<Button
android:id="@+id/buttone2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttone1"
android:layout_marginTop="15dp"
android:padding="5dp"
android:text="Button2" />
<Button
android:id="@+id/buttone3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttone1"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="@+id/buttone2"
android:padding="5dp"
android:text="Button3" />
<Button
android:id="@+id/buttone4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/buttone2"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:padding="5dp"
android:text="Button4" />
<Button
android:id="@+id/buttone5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/buttone4"
android:layout_marginLeft="15dp"
android:layout_marginTop="50dp"
android:padding="5dp"
android:text="Button5" />
<Button
android:id="@+id/buttone6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button6" />
<Button
android:id="@+id/buttone7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="5dp"
android:text="Button7" />
</RelativeLayout>
RelativeLayout Properties
Reviewed by Anonymous
on
December 26, 2015
Rating:
No comments: