| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | page.title=Common Layout Objects |
| The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 2 | parent.title=User Interface |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3 | parent.link=index.html |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4 | @jd:body |
| 5 | |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 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="#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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 20 | <p>This section describes some of the more common types of layout objects |
| 21 | to use in your applications. Like all layouts, they are subclasses of {@link android.view.ViewGroup ViewGroup}.</p> |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | |
| The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 23 | <p>Also see the <a href="{@docRoot}guide/tutorials/views/index.html">Hello Views</a> tutorials for |
| 24 | some guidance on using more Android View layouts.</p> |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 25 | |
| 26 | <h2 id="framelayout">FrameLayout</h2> |
| 27 | <p>{@link android.widget.FrameLayout FrameLayout} is the simplest type of layout |
| 28 | object. It's basically a blank space on your screen that you can |
| 29 | later fill with a single object — for example, a picture that you'll swap in and out. |
| 30 | All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot |
| 31 | specify a different location for a child view. Subsequent child views will simply be drawn over previous ones, |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | partially or totally obscuring them (unless the newer object is transparent). |
| 33 | </p> |
| 34 | |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 36 | <h2 id="linearlayout">LinearLayout</h2> |
| 37 | <p>{@link android.widget.LinearLayout LinearLayout} aligns all children in a |
| 38 | single direction — vertically or horizontally, depending on how you |
| 39 | define the <code>orientation</code> attribute. All children are |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | stacked one after the other, so a vertical list will only have one child per |
| 41 | row, no matter how wide they are, and a horizontal list will only be one row |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 42 | high (the height of the tallest child, plus padding). A {@link |
| 43 | android.widget.LinearLayout LinearLayout} respects <em>margin</em>s between children |
| 44 | and the <em>gravity</em> (right, center, or left alignment) of each child. </p> |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | |
| 46 | <p>{@link android.widget.LinearLayout LinearLayout} also supports assigning a |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 47 | <em>weight</em> to individual children. This attribute assigns an "importance" value to a view, |
| 48 | and allows it to expand to fill any remaining space in the parent view. |
| 49 | Child views can specify an integer weight value, and then any remaining space in the view group is |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | assigned to children in the proportion of their declared weight. Default |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 51 | weight is zero. For example, if there are three text boxes and two of |
| 52 | them declare a weight of 1, while the other is given no weight (0), the third text box without weight |
| 53 | will not grow and will only occupy the area required by its content. |
| 54 | The other two will expand equally to fill the space remaining after all three boxes are measured. |
| 55 | If 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 |
| 57 | share the rest equally.</p> |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 58 | |
| 59 | <div class="sidebox"> |
| 60 | <p><strong>Tip</strong>: To create a proportionate size |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 61 | layout 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 |
| 63 | the 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | to each child, depending on what proportion of the screen each should |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 66 | have.</p> |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 67 | </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 Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 70 | button, some labels and text boxes. The text boxes have their width set to <var>fill_parent</var>; other |
| 71 | elements are set to <var>wrap_content</var>. The gravity, by default, is left. |
| 72 | The difference between the two versions of the form is that the form |
| 73 | on the left has weight values unset (0 by default), while the form on the right has |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | the comments text box weight set to 1. If the Name textbox had also been set |
| 75 | to 1, the Name and Comments text boxes would be the same height. </p> |
| 76 | |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 77 | <img src="{@docRoot}images/linearlayout.png" alt="" /> |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | |
| 79 | <p>Within a horizontal {@link android.widget.LinearLayout LinearLayout}, items are aligned by the position of |
| 80 | their text base line (the first line of the first list element — topmost or |
| 81 | leftmost — is considered the reference line). This is so that people scanning |
| 82 | elements in a form shouldn't have to jump up and down to read element text in |
| 83 | neighboring elements. This can be turned off by setting |
| 84 | <code>android:baselineAligned="false"</code> in the layout XML. </p> |
| 85 | |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 86 | <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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 88 | |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 89 | |
| 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 |
| 94 | cells 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). |
| 97 | Each 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 |
| 98 | composed of a variety of View objects, like ImageView or TextView objects. |
| 99 | A cell may also be a ViewGroup object (for example, you can nest another TableLayout as a cell).</p> |
| The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 100 | <p>The following sample layout has two rows and two cells in each. The accompanying screenshot shows the |
| 101 | result, with cell borders displayed as dotted lines (added for visual effect). </p> |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 102 | |
| The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 103 | <table class="columns"> |
| 104 | <tr> |
| 105 | <td> |
| 106 | <pre> |
| 107 | <?xml version="1.0" encoding="utf-8"?> |
| 108 | <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 | <TableRow> |
| 113 | <TextView |
| 114 | android:text="@string/table_layout_4_open" |
| 115 | android:padding="3dip" /> |
| 116 | <TextView |
| 117 | android:text="@string/table_layout_4_open_shortcut" |
| 118 | android:gravity="right" |
| 119 | android:padding="3dip" /> |
| 120 | </TableRow> |
| 121 | |
| 122 | <TableRow> |
| 123 | <TextView |
| 124 | android:text="@string/table_layout_4_save" |
| 125 | android:padding="3dip" /> |
| 126 | <TextView |
| 127 | android:text="@string/table_layout_4_save_shortcut" |
| 128 | android:gravity="right" |
| 129 | android:padding="3dip" /> |
| 130 | </TableRow> |
| 131 | </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 Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 136 | |
| 137 | <p>Columns can be hidden, marked to stretch and fill the available screen space, |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | or can be marked as shrinkable to force the column to shrink until the table |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 139 | fits the screen. See the {@link android.widget.TableLayout TableLayout reference} |
| 140 | documentation for more details. </p> |
| 141 | |
| 142 | <p>To view sample code, see the <a href="{@docRoot}guide/tutorials/views/hello-tablelayout.html">Hello |
| 143 | TableLayout</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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | 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 Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 152 | to use it, because it is fairly rigid and does not adjust to different types of |
| The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 153 | displays. </p> |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 154 | |
| 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 161 | is centered in the screen, other elements aligning themselves to that element |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 162 | 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 |
| 164 | file before you refer to it from the other views via its reference ID. </p> |
| The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 165 | <p>The example below shows an XML file and the resulting screen in the UI. |
| 166 | Note that the attributes that refer to relative elements (e.g., <var>layout_toLeft</var>) |
| 167 | refer to the ID using the syntax of a relative resource |
| 168 | (<var>@id/<em>id</em></var>). </p> |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 169 | |
| The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 170 | <table class="columns"> |
| 171 | <tr> |
| 172 | <td> |
| 173 | <pre> |
| 174 | <?xml version="1.0" encoding="utf-8"?> |
| 175 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android |
| 176 | android:layout_width="fill_parent" |
| 177 | android:layout_height="wrap_content" |
| 178 | android:background="@drawable/blue" |
| 179 | android:padding="10px" > |
| The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 180 | |
| The Android Open Source Project | d24b818 | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 181 | <TextView android:id="@+id/label" |
| 182 | android:layout_width="fill_parent" |
| 183 | android:layout_height="wrap_content" |
| 184 | android:text="Type here:" /> |
| 185 | |
| 186 | <EditText android:id="@+id/entry" |
| 187 | android:layout_width="fill_parent" |
| 188 | android:layout_height="wrap_content" |
| 189 | android:background="@android:drawable/editbox_background" |
| 190 | android:layout_below="@id/label" /> |
| 191 | |
| 192 | <Button android:id="@+id/ok" |
| 193 | android:layout_width="wrap_content" |
| 194 | android:layout_height="wrap_content" |
| 195 | android:layout_below="@id/entry" |
| 196 | android:layout_alignParentRight="true" |
| 197 | android:layout_marginLeft="10px" |
| 198 | android:text="OK" /> |
| 199 | |
| 200 | <Button android:layout_width="wrap_content" |
| 201 | android:layout_height="wrap_content" |
| 202 | android:layout_toLeftOf="@id/ok" |
| 203 | android:layout_alignTop="@id/ok" |
| 204 | android:text="Cancel" /> |
| 205 | </RelativeLayout> |
| 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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 213 | 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 Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 215 | 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 — 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 |
| 223 | RelativeLayout</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 Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 229 | <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> |