blob: 095b0dd0d9ade062ad48a76ec75e30ec84059d34 [file] [log] [blame]
Scott Main569ed222011-12-02 13:49:44 -08001page.title=Re-using Layouts with <include/>
Scott Main580f0142011-12-15 16:47:26 -08002parent.title=Improving Layout Performance
Scott Main569ed222011-12-02 13:49:44 -08003parent.link=index.html
4
5trainingnavtop=true
6previous.title=Optimizing Layout Hierarchies
7previous.link=optimizing-layout.html
8next.title=Loading Views On Demand
9next.link=loading-ondemand.html
10
11@jd:body
12
13
14<div id="tb-wrapper">
15<div id="tb">
16
17<!-- table of contents -->
18<h2>This lesson teaches you to</h2>
19<ol>
20 <li><a href="#Create">Create a Re-usable Layout</a></li>
21 <li><a href="#Include">Use the &lt;include&gt; Tag</a></li>
22 <li><a href="#Merge">Use the &lt;merge&gt; Tag</a></li>
23</ol>
24
25<!-- other docs (NOT javadocs) -->
26<h2>You should also read</h2>
27<ul>
28 <li><a href="{@docRoot}resources/articles/layout-tricks-reuse.html">Creating Reusable UI
29Components</a></li>
30 <li><a href="{@docRoot}resources/articles/layout-tricks-merge.html">Merging Layouts</a></li>
31 <li><a
32href="{@docRoot}guide/topics/resources/layout-resource.html#include-element">Layout
33Resource</a></li>
34</ul>
35
36</div>
37</div>
38
39
40
41<p>Although Android offers a variety of widgets to provide small and re-usable interactive elements,
42you might also need to re-use larger components that require a special layout. To efficiently
43re-use complete layouts, you can use the {@code &lt;include/&gt;} and {@code &lt;merge/&gt;} tags
44to embed another layout inside the current layout.</p>
45
46<p>Reusing layouts is particularly powerful as it allows you create reusable complex layouts. For
47example, a yes/no button panel, or custom progress bar with description text.
48It also means that any elements of your application that are common across multiple layouts can be
49extracted, managed separately, then included in each layout. So while
50you can create individual UI components by writing a custom {@link android.view.View}, you can
51do it even more easily by re-using a layout file.</p>
52
53
54<h2 id="Create">Create a Re-usable Layout</h2>
55
56<p>If you already know the layout that you want to re-use, create a new XML file and define the
57layout. For example, here's a layout from the G-Kenya codelab that defines a title bar to be
58included in each activity (<code>titlebar.xml</code>):</p>
59
60<pre>
61&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
62 android:layout_width=”match_parent”
63 android:layout_height="wrap_content"
64 android:background="&#64;color/titlebar_bg">
65
66 &lt;ImageView android:layout_width="wrap_content"
67 android:layout_height="wrap_content"
68 android:src="&#64;drawable/gafricalogo" />
69&lt;/FrameLayout>
70</pre>
71
72<p>The root {@link android.view.View} should be exactly how you'd like it to appear in each
73layout to which you add this layout.</p>
74
75
76<h2 id="Include">Use the &lt;include&gt; Tag</h2>
77
78<p>Inside the layout to which you want to add the re-usable component, add the {@code
79&lt;include/&gt;} tag. For example, here's a layout from the
80G-Kenya codelab that includes the title bar from above:</p>
81
82<p>Here's the layout file:</p>
83
84<pre>
85&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
86 android:orientation="vertical"
87 android:layout_width=”match_parent
88 android:layout_height=”match_parent
89 android:background="&#64;color/app_bg"
90 android:gravity="center_horizontal">
91
92 <strong>&lt;include layout="&#64;layout/titlebar"/></strong>
93
94 &lt;TextView android:layout_width=”match_parent
95 android:layout_height="wrap_content"
96 android:text="&#64;string/hello"
97 android:padding="10dp" />
98
99 ...
100
101&lt;/LinearLayout>
102</pre>
103
104<p>You can also override all the layout parameters (any {@code android:layout_*} attributes) of the
105included layout's root view by specifying them in the {@code &lt;include/&gt;} tag. For
106example:</p>
107
108<pre>
109&lt;include android:id=”&#64;+id/news_title”
110 android:layout_width=”match_parent”
111 android:layout_height=”match_parent”
112 layout=”@layout/title”/>
113</pre>
114
115
116
117<h2 id="Merge">Use the &lt;merge&gt; Tag</h2>
118
119<p>The {@code &lt;merge />} tag helps eliminate redundant view groups in your view hierarchy
120when including one layout within another. For example, if your main layout is a vertical {@link
121android.widget.LinearLayout} in which two consecutive views can be
122re-used in multiple layouts, then the re-usable layout in which you place the two views requires its
123own root view. However, using another {@link android.widget.LinearLayout} as the root for the
124re-usable layout would result in a vertical {@link android.widget.LinearLayout} inside a
125vertical {@link android.widget.LinearLayout}. The nested {@link android.widget.LinearLayout}
126serves no real purpose other than to slow down your UI performance.</p>
127
128<p>To avoid including such a redundant view group, you can instead use the
129{@code &lt;merge&gt;} element as the root view for the re-usable layout. For example:</p>
130
131<pre>
132&lt;merge xmlns:android="http://schemas.android.com/apk/res/android">
133
134 &lt;Button
135 android:layout_width="fill_parent"
136 android:layout_height="wrap_content"
137 android:text="@string/add"/>
138
139 &lt;Button
140 android:layout_width="fill_parent"
141 android:layout_height="wrap_content"
142 android:text="@string/delete"/>
143
144&lt;/merge>
145</pre>
146
147<p>Now, when you include this layout in another layout (using the {@code &lt;include/&gt;} tag), the
148system ignores the {@code &lt;merge&gt;} element and places the two buttons directly in the
149layout, in place of the {@code &lt;include/&gt;} tag.</p>
150