Original article, if reproduced, please indicate the source: http://blog.csdn.net/yihui823/article/details/6702312
Linear layout. Simply put, this layout means that all controls are sorted in sequence and no one will cover them. The linear layout needs to define a direction, horizontal (Android: Orientation = "horizontal") or vertical (Android: Orientation = "vertical "). That is to say, the controls are either arranged side by side horizontally or vertically.
Layoutpic008
The following is a straight line: layoutpic009
Because they are placed side by side, they will not overlap.
Let's change the example in the previous chapter to linearlayout:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: Id = "@ + ID/TV1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textsize = "50dip"
Android: textcolor = "# ffffff"
Android: text = "first layer"/>
<Textview
Android: Id = "@ + ID/TV2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textsize = "40dip"
Android: textcolor = "# FFFF00"
Android: layout_torightof = "@ ID/TV1"
Android: text = "Layer 2"/>
<Textview
Android: Id = "@ + ID/TV3"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textsize = "30dip"
Android: textcolor = "# ff00ff"
Android: gravity = "right"
Android: text = "Layer 3"/>
<Textview
Android: Id = "@ + ID/TV4"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textsize = "20dip"
Android: textcolor = "#00 FFFF"
Android: layout_centervertical = "true"
Android: text = "Layer 4"/>
</Linearlayout>
The effect is as follows: layoutpic003
Change 1
Let's change it to horizontal arrangement. Change only one row:
Android: Orientation = "horizontal"
The effect is as follows:
Layoutpic004
Dizzy? Did you find something wrong? If you can't do it, go out and smoke.
Oh, forget that I don't smoke. In fact, it is very simple. The property of our control: Android: layout_width = "fill_parent" means that the width of the first text box is full of the screen, and the second text box is placed on the right of the first text box, naturally, you cannot see it.
If you know the reason, change it. Change the width attribute of the four text boxes:
Android: layout_width = "wrap_content"
Let's look at the effect: layoutpic005
Well. Because the text is too wide, the third text box is displayed in multiple lines for full display. The fourth text box disappears directly out of the screen. Press "Ctrl + F11" to bring the virtual machine to the ground and you will see:
Layoutpic006
Change 2
The screen remains unchanged. We try to change the second text box:
<Textview
Android: Id = "@ + ID/TV2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textsize = "40dip"
Android: textcolor = "# FFFF00"
Android: layout_below = "@ ID/TV1"
Android: text = "Layer 2"/>
Let's see if we can make the second text box under the first text box. The result indicates: no change.
Change 3
We try to change the third text box with the margin:
<Textview
Android: Id = "@ + ID/TV3"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textsize = "30dip"
Android: textcolor = "# ff00ff"
Android: gravity = "right"
Android: layout_margin = "20dip"
Android: text = "Layer 3"/>
Is there any effect? Yes:
Layoutpic007
The third text box maintains a certain distance from other controls and the edge of the parent control. (20 dip is not necessarily 20 pixels. I will describe this relationship carefully in the subsequent articles .)
Summary
Linear layout also does not directly control the specific position of the control and the relative positional relationship. Each widget is placed in sequence. However, the spacing between controls can be adjusted, and the controls do not overwrite each other. Linear layout can be nested, and a horizontal layout can be added to a vertical layout. With this nested method, you can complete some slightly complex pages. However, when Nesting is used more and more layers of nesting are used, maintenance will be very troublesome. At this time, we need to use a more complex layout.