1page.title=Linear Layout
2parent.title=Layouts
3parent.link=layout-objects.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="#Weight">Layout Weight</a></li>
11  <li><a href="#Example">Example</a></li>
12</ol>
13
14<h2>Key classes</h2>
15<ol>
16  <li>{@link android.widget.LinearLayout}</li>
17  <li>{@link android.widget.LinearLayout.LayoutParams}</li>
18</ol>
19</div>
20</div>
21
22<p>{@link android.widget.LinearLayout} is a view group that aligns all children in a single
23direction, vertically or horizontally. You can specify the layout direction with the
24<a href="{@docRoot}reference/android/widget/LinearLayout.html#attr_android:orientation">{@code
25android:orientation}</a> attribute.</p>
26
27<img src="{@docRoot}images/ui/linearlayout.png" alt="" />
28
29<p>All children of a {@link android.widget.LinearLayout} are
30stacked one after the other, so a vertical list will only have one child per
31row, no matter how wide they are, and a horizontal list will only be one row
32high (the height of the tallest child, plus padding). A {@link
33android.widget.LinearLayout LinearLayout} respects <em>margin</em>s between children
34and the <em>gravity</em> (right, center, or left alignment) of each child. </p>
35
36
37<h2 id="Weight">Layout Weight</h2>
38
39<div class="sidebox-wrapper">
40<div class="sidebox">
41  <h3>Equally weighted children</h3>
42<p>To create a linear layout in which each child uses the same amount of
43space on the screen, set the <a
44href="{@docRoot}reference/android/view/ViewGroup.LayoutParams.html#attr_android:layout_height"
45>{@code android:layout_height}</a> of each view to {@code "0dp"} (for a
46vertical layout) or the <a
47href="{@docRoot}reference/android/view/ViewGroup.LayoutParams.html#attr_android:layout_width"
48>{@code android:layout_width}</a> of each view to {@code "0dp"} (for a
49horizontal
50layout). Then set the <a
51href="{@docRoot}reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_weight"
52>{@code android:layout_weight}</a> of each view to {@code "1"}.</p>
53</div>
54</div>
55
56
57<p>{@link android.widget.LinearLayout} also supports assigning a
58<em>weight</em> to individual children with the <a
59href="{@docRoot}reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_weight"
60>{@code android:layout_weight}</a> attribute.
61This attribute assigns an "importance" value to a view in
62terms of how much space is should occupy on the screen. A larger weight value allows it to expand
63to fill any remaining space in the parent view. 
64Child views can specify a weight value, and then any remaining space in the view group is
65assigned to children in the proportion of their declared weight. Default
66weight is zero.</p>
67
68<p>For example, if there are three text fields and two of them declare a weight of 1, while the
69other is given no weight, the third text field without weight will not grow and will only occupy the
70area required by its content. The other two will expand equally to fill the space remaining after
71all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then
72it is now declared more important than both the others, so it gets half the total remaining space,
73while the first two
74share the rest equally.</p>
75
76
77<h2 id="Example">Example</h2>
78
79<div class="figure" style="width:220px;margin-top:0">
80<img src="{@docRoot}images/ui/sample-linearlayout.png" alt="" />
81</div>
82
83<pre>
84&lt;?xml version="1.0" encoding="utf-8"?>
85&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
86    android:layout_width="fill_parent"
87    android:layout_height="fill_parent"
88    android:paddingLeft="16dp"
89    android:paddingRight="16dp"
90    android:orientation="vertical" >
91    &lt;EditText
92        android:layout_width="fill_parent"
93        android:layout_height="wrap_content"
94        android:hint="@string/to" />
95    &lt;EditText
96        android:layout_width="fill_parent"
97        android:layout_height="wrap_content"
98        android:hint="@string/subject" />
99    &lt;EditText
100        android:layout_width="fill_parent"
101        android:layout_height="0dp"
102        android:layout_weight="1"
103        android:gravity="top"
104        android:hint="@string/message" />
105    &lt;Button
106        android:layout_width="100dp"
107        android:layout_height="wrap_content"
108        android:layout_gravity="right"
109        android:text="@string/send" />
110&lt;/LinearLayout>
111</pre>
112
113<p>For details about the attributes available to each child view of a {@link
114android.widget.LinearLayout}, see {@link android.widget.LinearLayout.LayoutParams}.</p>
115
116
117