1 page.title=Linear Layout 2 parent.title=Layouts 3 parent.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 23 direction, vertically or horizontally. You can specify the layout direction with the 24 <a href="{@docRoot}reference/android/widget/LinearLayout.html#attr_android:orientation">{@code 25 android: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 30 stacked one after the other, so a vertical list will only have one child per 31 row, no matter how wide they are, and a horizontal list will only be one row 32 high (the height of the tallest child, plus padding). A {@link 33 android.widget.LinearLayout LinearLayout} respects <em>margin</em>s between children 34 and 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 43 space on the screen, set the <a 44 href="{@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 46 vertical layout) or the <a 47 href="{@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 49 horizontal 50 layout). Then set the <a 51 href="{@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 59 href="{@docRoot}reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_weight" 60 >{@code android:layout_weight}</a> attribute. 61 This attribute assigns an "importance" value to a view in 62 terms of how much space is should occupy on the screen. A larger weight value allows it to expand 63 to fill any remaining space in the parent view. 64 Child views can specify a weight value, and then any remaining space in the view group is 65 assigned to children in the proportion of their declared weight. Default 66 weight 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 69 other is given no weight, the third text field without weight will not grow and will only occupy the 70 area required by its content. The other two will expand equally to fill the space remaining after 71 all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then 72 it is now declared more important than both the others, so it gets half the total remaining space, 73 while the first two 74 share 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 <?xml version="1.0" encoding="utf-8"?> 85 <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 <EditText 92 android:layout_width="fill_parent" 93 android:layout_height="wrap_content" 94 android:hint="@string/to" /> 95 <EditText 96 android:layout_width="fill_parent" 97 android:layout_height="wrap_content" 98 android:hint="@string/subject" /> 99 <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 <Button 106 android:layout_width="100dp" 107 android:layout_height="wrap_content" 108 android:layout_gravity="right" 109 android:text="@string/send" /> 110 </LinearLayout> 111 </pre> 112 113 <p>For details about the attributes available to each child view of a {@link 114 android.widget.LinearLayout}, see {@link android.widget.LinearLayout.LayoutParams}.</p> 115 116 117