• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Common Layout Objects
2parent.title=User Interface
3parent.link=index.html
4@jd:body
5
6<div id="qv-wrapper">
7<div id="qv">
8  <h2>In this document</h2>
9  <ol>
10    <li><a href="#framelayout">FrameLayout</a></li>
11    <li><a href="#linearlayout">LinearLayout</a></li>
12    <li><a href="#tablelayout">TableLayout</a></li>
13    <li><a href="#relativelayout">RelativeLayout</a></li>
14    <li><a href="#viewgroupsummary">Summary of Important View Groups</a></li>
15  </ol>
16</div>
17</div>
18
19<p>This section describes some of the more common types of layout objects
20to use in your applications. Like all layouts, they are subclasses of {@link android.view.ViewGroup ViewGroup}.</p>
21
22<p>Also see the <a href="{@docRoot}resources/tutorials/views/index.html">Hello Views</a> tutorials for
23some guidance on using more Android View layouts.</p>
24
25<h2 id="framelayout">FrameLayout</h2>
26<p>{@link android.widget.FrameLayout FrameLayout} is the simplest type of layout
27object. It's basically a blank space on your screen that you can
28later fill with a single object &mdash; for example, a picture that you'll swap in and out.
29All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot
30specify a different location for a child view. Subsequent child views will simply be drawn over previous ones,
31partially or totally obscuring them (unless the newer object is transparent).
32</p>
33
34
35<h2 id="linearlayout">LinearLayout</h2>
36<p>{@link android.widget.LinearLayout LinearLayout} aligns all children in a
37single direction &mdash; vertically or horizontally, depending on how you
38define the <code>orientation</code> attribute. All children are
39stacked one after the other, so a vertical list will only have one child per
40row, no matter how wide they are, and a horizontal list will only be one row
41high (the height of the tallest child, plus padding). A {@link
42android.widget.LinearLayout LinearLayout} respects <em>margin</em>s between children
43and the <em>gravity</em> (right, center, or left alignment) of each child. </p>
44
45<p>{@link android.widget.LinearLayout LinearLayout} also supports assigning a
46<em>weight</em> to individual children. This attribute assigns an "importance" value to a view,
47and allows it to expand to fill any remaining space in the parent view.
48Child views can specify an integer weight value, and then any remaining space in the view group is
49assigned to children in the proportion of their declared weight. Default
50weight is zero. For example, if there are three text boxes and two of
51them declare a weight of 1, while the other is given no weight (0), the third text box without weight
52will not grow and will only occupy the area required by its content.
53The other two will expand equally to fill the space remaining after all three boxes are measured.
54If the third box is then given a weight of 2 (instead of 0), then it is now declared
55"more important" than both the others, so it gets half the total remaining space, while the first two
56share the rest equally.</p>
57
58<div class="sidebox-wrapper">
59<div class="sidebox">
60<p><strong>Tip</strong>: To create a proportionate size
61layout on the screen, create a container view group object with the
62<code>layout_width</code> and <code>layout_height</code> attributes set to <var>fill_parent</var>; assign
63the children <code>height</code> or <code>width</code> to <code>0</code> (zero); then assign relative
64<code>weight</code> values
65to each child, depending on what proportion of the screen each should
66have.</p>
67</div>
68</div>
69
70<p>The following two forms represent a {@link android.widget.LinearLayout LinearLayout} with a set of elements: a
71button, some labels and text boxes. The text boxes have their width set to <var>fill_parent</var>; other
72elements are set to <var>wrap_content</var>. The gravity, by default, is left.
73The difference between the two versions of the form is that the form
74on the left has weight values unset (0 by default), while the form on the right has
75the comments text box weight set to 1. If the Name textbox had also been set
76to 1, the Name and Comments text boxes would be the same height. </p>
77
78<img src="{@docRoot}images/linearlayout.png" alt="" />
79
80<p>Within a horizontal {@link android.widget.LinearLayout LinearLayout}, items are aligned by the position of
81their text base line (the first line of the first list element &mdash; topmost or
82leftmost &mdash; is considered the reference line). This is so that people scanning
83elements in a form shouldn't have to jump up and down to read element text in
84neighboring elements. This can be turned off by setting
85<code>android:baselineAligned=&quot;false&quot;</code> in the layout XML. </p>
86
87<p>To view other sample code, see the
88<a href="{@docRoot}resources/tutorials/views/hello-linearlayout.html">Hello LinearLayout</a> tutorial.</p>
89
90
91<h2 id="tablelayout">TableLayout</h2>
92<p>{@link android.widget.TableLayout} positions its children into rows
93    and columns. TableLayout containers do not display border lines for their rows, columns,
94    or cells. The table will have as many columns as the row with the most cells. A table can leave
95cells empty, but cells cannot span columns, as they can in HTML.</p>
96<p>{@link android.widget.TableRow} objects are the child views of a TableLayout
97(each TableRow defines a single row in the table).
98Each row has zero or more cells, each of which is defined by any kind of other View. So, the cells of a row may be
99composed of a variety of View objects, like ImageView or TextView objects.
100A cell may also be a ViewGroup object (for example, you can nest another TableLayout as a cell).</p>
101<p>The following sample layout has two rows and two cells in each. The accompanying screenshot shows the
102result, with cell borders displayed as dotted lines (added for visual effect). </p>
103
104<table class="columns">
105    <tr>
106        <td>
107        <pre>
108&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
109&lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
110    android:layout_width="fill_parent"
111    android:layout_height="fill_parent"
112    android:stretchColumns="1">
113    &lt;TableRow>
114        &lt;TextView
115            android:text="@string/table_layout_4_open"
116            android:padding="3dip" />
117        &lt;TextView
118            android:text="@string/table_layout_4_open_shortcut"
119            android:gravity="right"
120            android:padding="3dip" />
121    &lt;/TableRow>
122
123    &lt;TableRow>
124        &lt;TextView
125            android:text="@string/table_layout_4_save"
126            android:padding="3dip" />
127        &lt;TextView
128            android:text="@string/table_layout_4_save_shortcut"
129            android:gravity="right"
130            android:padding="3dip" />
131    &lt;/TableRow>
132&lt;/TableLayout>
133</pre></td>
134        <td><img src="{@docRoot}images/table_layout.png" alt="" style="margin:0" /></td>
135    </tr>
136</table>
137
138<p>Columns can be hidden, marked to stretch and fill the available screen space,
139    or can be marked as shrinkable to force the column to shrink until the table
140    fits the screen. See the {@link android.widget.TableLayout TableLayout reference}
141documentation for more details. </p>
142
143<p>To view sample code, see the <a href="{@docRoot}resources/tutorials/views/hello-tablelayout.html">Hello
144TableLayout</a> tutorial.</p>
145
146
147<h2 id="relativelayout">RelativeLayout</h2>
148<p>{@link android.widget.RelativeLayout} lets child views specify their
149    position relative to the parent view or to each other (specified by ID). So you can
150    align two elements by right border, or make one below another, centered in
151    the screen, centered left, and so on. Elements are rendered in the order given, so if the first element
152    is centered in the screen, other elements aligning themselves to that element
153    will be aligned relative to screen center. Also, because of this ordering, if using XML to specify this layout,
154    the element that you will reference (in order to position other view objects) must be listed in the XML
155file before you refer to it from the other views via its reference ID. </p>
156<p>The example below shows an XML file and the resulting screen in the UI.
157Note that the attributes that refer to relative elements (e.g., <var>layout_toLeft</var>)
158refer to the ID using the syntax of a relative resource
159(<var>@id/<em>id</em></var>). </p>
160
161<table class="columns">
162    <tr>
163        <td>
164        <pre>
165&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
166&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android
167                android:layout_width=&quot;fill_parent&quot;
168                android:layout_height=&quot;wrap_content&quot;
169                android:background=&quot;@drawable/blue&quot;
170                android:padding=&quot;10px&quot; &gt;
171
172    &lt;TextView android:id=&quot;@+id/label&quot;
173              android:layout_width=&quot;fill_parent&quot;
174              android:layout_height=&quot;wrap_content&quot;
175              android:text=&quot;Type here:&quot; /&gt;
176
177    &lt;EditText android:id=&quot;@+id/entry&quot;
178              android:layout_width=&quot;fill_parent&quot;
179              android:layout_height=&quot;wrap_content&quot;
180              android:background=&quot;@android:drawable/editbox_background&quot;
181              android:layout_below=&quot;@id/label&quot; /&gt;
182
183    &lt;Button android:id=&quot;@+id/ok&quot;
184            android:layout_width=&quot;wrap_content&quot;
185            android:layout_height=&quot;wrap_content&quot;
186            android:layout_below=&quot;@id/entry&quot;
187            android:layout_alignParentRight=&quot;true&quot;
188            android:layout_marginLeft=&quot;10px&quot;
189            android:text=&quot;OK&quot; /&gt;
190
191    &lt;Button android:layout_width=&quot;wrap_content&quot;
192            android:layout_height=&quot;wrap_content&quot;
193            android:layout_toLeftOf=&quot;@id/ok&quot;
194            android:layout_alignTop=&quot;@id/ok&quot;
195            android:text=&quot;Cancel&quot; /&gt;
196&lt;/RelativeLayout&gt;
197</pre></td>
198        <td><img src="{@docRoot}images/designing_ui_layout_example.png" alt="" style="margin:0"  /></td>
199    </tr>
200</table>
201
202
203<p>Some of these properties are supported directly by
204    the element, and some are supported by its LayoutParams member (subclass RelativeLayout
205    for all the elements in this screen, because all elements are children of a RelativeLayout
206    parent object). The defined RelativeLayout parameters are: <code>width</code>, <code>height</code>,
207    <code>below</code>, <code>alignTop</code>, <code>toLeft</code>, <code>padding[Bottom|Left|Right|Top]</code>,
208    and <code>margin[Bottom|Left|Right|Top]</code>. Note that some of these parameters specifically support
209    relative layout positions &mdash; their values must be the ID of the element to which you'd like this view laid relative.
210    For example, assigning the parameter <code>toLeft="my_button"</code> to a TextView would place the TextView to
211    the left of the View with the ID <var>my_button</var> (which must be written in the XML <em>before</em> the TextView). </p>
212
213<p>To view this sample code, see the <a href="{@docRoot}resources/tutorials/views/hello-relativelayout.html">Hello
214RelativeLayout</a> tutorial.</p>
215
216
217<h2 id="viewgroupsummary">Summary of Important View Groups</h2>
218<p>These objects all hold child UI elements. Some provide their own form of a visible UI, while others
219    are invisible structures that only manage the layout of their child views.  </p>
220<table width="100%" border="1">
221    <tr>
222        <th scope="col">Class</th>
223        <th scope="col">Description</th>
224    </tr>
225    <tr>
226        <td>{@link android.widget.FrameLayout FrameLayout}</td>
227        <td>Layout that acts as a view frame to display
228            a single object. </td>
229    </tr>
230    <tr>
231        <td>{@link android.widget.Gallery Gallery} </td>
232        <td>A horizontal scrolling display of images, from a bound list. </td>
233    </tr>
234    <tr>
235        <td>{@link android.widget.GridView GridView} </td>
236        <td>Displays a scrolling grid of m columns and n rows.</td>
237    </tr>
238    <tr>
239        <td>{@link android.widget.LinearLayout LinearLayout} </td>
240        <td>A layout that organizes its children into a single horizontal or vertical
241            row. It creates a scrollbar if the length of the window exceeds the length
242            of the screen. </td>
243    </tr>
244    <tr>
245        <td>{@link android.widget.ListView ListView} </td>
246        <td>Displays a scrolling single column list. </td>
247    </tr>
248    <tr>
249        <td>{@link android.widget.RelativeLayout RelativeLayout} </td>
250        <td>Enables you to specify the location of child objects relative to each
251            other (child A to the left of child B) or to the parent (aligned to the
252            top of the parent). </td>
253    </tr>
254    <tr>
255        <td>{@link android.widget.ScrollView ScrollView} </td>
256        <td>A vertically scrolling column of elements. </td>
257    </tr>
258    <tr>
259        <td>{@link android.widget.Spinner Spinner} </td>
260        <td>Displays a single item at a time from a bound list, inside a one-row
261            textbox. Rather like a one-row listbox that can scroll either horizontally
262            or vertically. </td>
263    </tr>
264    <tr>
265        <td>{@link android.view.SurfaceView SurfaceView} </td>
266        <td>Provides direct access to a dedicated drawing surface. It can hold child
267            views layered on top of the surface, but is intended for applications
268            that need to draw pixels, rather than using widgets. </td>
269    </tr>
270    <tr>
271        <td>{@link android.widget.TabHost TabHost} </td>
272        <td>Provides a tab selection list that monitors clicks and enables the application
273            to change the screen whenever a tab is clicked. </td>
274    </tr>
275	<tr>
276        <td>{@link android.widget.TableLayout TableLayout} </td>
277        <td>A tabular layout with an arbitrary number of rows and columns, each cell
278            holding the widget of your choice. The rows resize to fit the largest
279            column. The cell borders are not
280            visible. </td>
281    </tr>
282    <tr>
283        <td>{@link android.widget.ViewFlipper ViewFlipper} </td>
284        <td>A list that displays one item at a time, inside a one-row textbox. It
285            can be set to swap items at timed intervals, like a slide show. </td>
286    </tr>
287    <tr>
288        <td>{@link android.widget.ViewSwitcher ViewSwitcher} </td>
289        <td>Same as ViewFlipper. </td>
290    </tr>
291</table>
292