| Dirk Dougherty | 22558d0 | 2009-12-10 16:25:06 -0800 | [diff] [blame] | 1 | page.title=Layout Tricks: Creating Reusable UI Components |
| Scott Main | 796ce77 | 2011-02-16 10:04:45 -0800 | [diff] [blame] | 2 | parent.title=Articles |
| 3 | parent.link=../browser.html?tag=article |
| Dirk Dougherty | 22558d0 | 2009-12-10 16:25:06 -0800 | [diff] [blame] | 4 | @jd:body |
| 5 | |
| 6 | <p>The Android platform offers a wide variety of UI <em>widgets</em>, small |
| 7 | visual construction blocks that you can glue together to present users with |
| 8 | complex and useful interfaces. However applications often need higher-level |
| 9 | visual <em>components</em>. To meet that need, and to do so efficiently, you can |
| 10 | combine multiple standard widgets into a single, reusable component. </p> |
| 11 | |
| 12 | <p>For example, you could create a reusable component that contains a progress |
| 13 | bar and a cancel button, a panel containing two buttons (positive and negative |
| 14 | actions), a panel with an icon, a title and a description, and so on. You can |
| 15 | create UI components easily by writing a custom <code>View</code>, but you can |
| 16 | do it even more easily using only XML.</p> |
| 17 | |
| 18 | <p>In Android XML layout files, each tag is mapped to an actual class instance |
| 19 | (the class is always a subclass of {@link android.view.View} The UI toolkit lets |
| 20 | you also use three special tags that are not mapped to a <code>View</code> |
| 21 | instance: <code><requestFocus /></code>, <code><merge /></code> and |
| 22 | <code><include /></code>. This article shows how to use <code><include |
| 23 | /></code> to create pure XML visual components. For information about how to |
| 24 | use <code><merge /></code>, which can be particularly powerful when |
| 25 | combined with <code><include /></code>see the <a |
| 26 | href="{@docRoot}resources/articles/layout-tricks-merge.html">Merging Layouts</a> |
| 27 | article. </p> |
| 28 | |
| 29 | <p>The <code><include /></code> element does exactly what its name |
| 30 | suggests; it includes another XML layout. Using this tag is straightforward as |
| 31 | shown in the following example, taken straight from <a |
| 32 | href="http://android.git.kernel.org/?p=platform/packages/apps/Launcher.git;a= |
| 33 | tree;h=refs/heads/master;hb=master">the source code of the Home application</a> |
| 34 | that ships with Android:</p> |
| 35 | |
| 36 | <pre class="prettyprint"><com.android.launcher.Workspace |
| 37 | android:id="@+id/workspace" |
| 38 | android:layout_width="fill_parent" |
| 39 | android:layout_height="fill_parent" |
| 40 | |
| 41 | launcher:defaultScreen="1"> |
| 42 | |
| 43 | <include android:id="@+id/cell1" layout="@layout/workspace_screen" /> |
| 44 | <include android:id="@+id/cell2" layout="@layout/workspace_screen" /> |
| 45 | <include android:id="@+id/cell3" layout="@layout/workspace_screen" /> |
| 46 | |
| 47 | </com.android.launcher.Workspace></pre> |
| 48 | |
| 49 | <p>In the <code><include /></code> only the <code>layout</code> attribute |
| 50 | is required. This attribute, without the <code>android</code> namespace prefix, |
| 51 | is a reference to the layout file you wish to include. In this example, the same |
| 52 | layout is included three times in a row. This tag also lets you override a few |
| 53 | attributes of the included layout. The above example shows that you can use |
| 54 | <code>android:id</code> to specify the id of the root view of the included |
| 55 | layout; it will also override the id of the included layout if one is defined. |
| 56 | Similarly, you can override all the layout parameters. This means that any |
| 57 | <code>android:layout_*</code> attribute can be used with the <code><include |
| Scott Main | 3577f51 | 2010-11-24 11:24:33 -0800 | [diff] [blame] | 58 | /></code> tag. Here is an example in |
| 59 | which the same layout is included twice, but only the first one overrides the layout properties:</p> |
| Dirk Dougherty | 22558d0 | 2009-12-10 16:25:06 -0800 | [diff] [blame] | 60 | |
| Scott Main | 3577f51 | 2010-11-24 11:24:33 -0800 | [diff] [blame] | 61 | <pre> |
| 62 | <!-- override the layout height and width --> |
| 63 | <include layout="@layout/image_holder" |
| 64 | android:layout_height="fill_parent" |
| 65 | android:layout_width="fill_parent" /> |
| 66 | <!-- do not override layout dimensions; inherit them from image_holder --> |
| 67 | <include layout="@layout/image_holder" /> |
| Dirk Dougherty | 22558d0 | 2009-12-10 16:25:06 -0800 | [diff] [blame] | 68 | </pre> |
| 69 | |
| Scott Main | 3577f51 | 2010-11-24 11:24:33 -0800 | [diff] [blame] | 70 | <p class="caution"><strong>Caution:</strong> If you want to override the layout dimensions, |
| 71 | you must override both <code>android:layout_height</code> and |
| 72 | <code>android:layout_width</code>—you cannot override only the height or only the width. |
| 73 | If you override only one, it will not take effect. (Other layout properties, such as weight, |
| 74 | are still inherited from the source layout.)</p> |
| 75 | |
| Dirk Dougherty | 22558d0 | 2009-12-10 16:25:06 -0800 | [diff] [blame] | 76 | <p>This tag is particularly useful when you need to customize only part of your |
| 77 | UI depending on the device's configuration. For instance, the main layout of |
| 78 | your activity can be placed in the <code>layout/</code> directory and can |
| 79 | include another layout which exists in two flavors, in <code>layout-land/</code> |
| 80 | and <code>layout-port/</code>. This allows you to share most of the UI in |
| 81 | portrait and landscape.</p> |