Linear Layout
The LinearLayout arranges views in a single column or a single row. Child views can be arranged either
vertically or horizontally. To see how LinearLayout works, consider the following elements typically
contained in the main.xml file:
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
Linear Layout
In the main.xml file, observe that the root element is and it has a element contained within it. The element controls the order in which the views contained within it appear. For example, the width of the element fi lls the entire width of its parent (which is the screen in this case) using the fill_parent constant. Its height is indicated by the wrap_content constant, which means that its height is the height of its content (in this case, the text contained within it). If you don’t want to have the view occupy the entire row, you can set its
layout_width attribute to wrap_content, like this:
Linear Layout
< TextView
android:layout_width=”wrap_content
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
This will set the width of the view to be equal to the width of the text contained within it.
Consider the following layout:
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
android:layout_width=”105dp”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
Here, you set the width of both the TextView and Button views to an absolute value. In this case, the
width for the TextView is set to 105 density-independent pixels wide, and the Button to 160
densityindependent pixels wide. Figure 3-1 shows how the views look when viewed on an emulator with a
resolution of 320×480.
As you can see, in both emulators the widths of both views are the same with respect to the width of
the emulator. This demonstrates the usefulness of using the dp unit, which ensures that even if the
resolution of the target device is different, the size of the view relative to the device remains
unchanged
The preceding example also specifies that the orientation of the layout is vertical:
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
The default orientation layout is horizontal, so if you omit the android:orientation attribute, the
views will appear as shown in Figure.
In LinearLayout, you can apply the layout_weight and layout_gravity attributes to views contained
within it, as the following modifications to main.xml show:
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
android:layout_width=”105dp”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
Figure shows that the button is aligned to the right of its parent (which is the LinearLayout) using
the layout_gravity attribute. At the same time, you use the layout_weight attribute to specify the
ratio in which the Button and EditText views occupy the remaining space on the screen. The total value
for the layout_weight attribute must be equal to 1.