0% found this document useful (0 votes)
423 views

Layout Cheat Sheet

The document describes different layout options in Android including vertical and horizontal LinearLayouts which arrange views in columns or rows, and how to set equal heights/widths or have one view take up leftover space. It also covers RelativeLayout which positions views relative to the parent layout or each other.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
423 views

Layout Cheat Sheet

The document describes different layout options in Android including vertical and horizontal LinearLayouts which arrange views in columns or rows, and how to set equal heights/widths or have one view take up leftover space. It also covers RelativeLayout which positions views relative to the parent layout or each other.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Layout

Vertical <LinearLayout
android:layout_width="match_parent"
LinearLayout android:layout_height="wrap_content"
android:orientation="vertical">
A vertical LinearLayout arranges
its children in a column. <TextView 1
android:layout_width="match_parent"
android:layout_height="wrap_content"
2
android:text="1"/>

3
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"/>

</LinearLayout>

Horizontal <LinearLayout
android:layout_width="wrap_content"
LinearLayout android:layout_height="match_parent"
android:orientation="horizontal">
A horizontal LinearLayout
arranges its children in a row. <TextView 1 2 3
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="2"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="3"/>

</LinearLayout>
Layout

Vertical <LinearLayout
android:layout_width="match_parent"
LinearLayout: android:layout_height="match_parent"

equal height
android:orientation="vertical">

<TextView 1
A vertical LinearLayout can give
android:layout_width="match_parent"
all of its children equal height. android:layout_height="0dp"
android:layout_weight="1"
android:text="1"/> 2

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
3
android:layout_weight="1"
android:text="2"/>

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="3"/>

</LinearLayout>

Vertical <LinearLayout
android:layout_width="match_parent"
LinearLayout: android:layout_height="match_parent"

leftover height
android:orientation="vertical">

<TextView 1
A vertical LinearLayout can give
android:layout_width="match_parent"
one of its children all the leftover android:layout_height="wrap_content" 2
height. android:layout_weight="0"
android:text="1"/>

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="2"/>
3
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="3"/>

</LinearLayout>
Layout

Horizontal <LinearLayout
android:layout_width="match_parent"
LinearLayout: android:layout_height="match_parent"

equal width
android:orientation="horizontal">

<TextView 1 2 3
A horizontal LinearLayout can
android:layout_width="0dp"
give all of its children equal width. android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"/>

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"/>

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"/>

</LinearLayout>

Horizontal <LinearLayout
android:layout_width="match_parent"
LinearLayout: android:layout_height="match_parent"

leftover width
android:orientation="horizontal">

<TextView 1 2 3
A horizontal LinearLayout can
android:layout_width="wrap_content"
give one of its children all the android:layout_height="match_parent"
leftover width. android:layout_weight="0"
android:text="1"/>

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0"
android:text="3"/>

</LinearLayout>
Layout

RelativeLayout <RelativeLayout
android:layout_width="match_parent"
A RelativeLayout can position android:layout_height="match_parent">

a child relative to the <TextView


RelativeLayout. android:layout_width="wrap_content"
1 2 3
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="1"/>

<TextView 4 Center 5
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="2"/>
6 7 8
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="3"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:text="4"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Center"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:text="5"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="6"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="7"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="8"/>

</RelativeLayout>
Layout

RelativeLayout <RelativeLayout
android:layout_width="match_parent"
A RelativeLayout can position a android:layout_height="match_parent">

child relative to another child that


<TextView
has an android:id attribute. android:id="@+id/center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Center"/> 1

2 Center 3
<TextView
android:layout_width="wrap_content" 4
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/center"
android:layout_alignBottom="@id/center"
android:text="2"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/center"
android:layout_alignBottom="@id/center"
android:text="3"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/center"
android:layout_alignLeft="@id/center"
android:text="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/center"
android:layout_alignLeft="@id/center"
android:text="4"/>

</RelativeLayout>

See the UI Overview and Layouts Guide. The attributes for the children of each type
of layout are listed in the LayoutParams classes.

For LinearLayout, see the LinearLayout Guide, class LinearLayout and its
source code, and class LinearLayout.LayoutParams and its source code.

For RelativeLayout, see the RelativeLayout guide, class RelativeLayout and


its source code, and class RelativeLayout.LayoutParams and its source code.

The Views in the above images are outlined only for clarity. Additional code would be
needed to draw the black borders and the margins between them.

*Code samples are licensed under the Apache 2.0 License. All other content is licensed under the Creative Commons Attribution 3.0 License.

You might also like