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