LinearLayout organizes views into one row or column. A child view can be arranged vertically or horizontally. To see how LinearLayout works, consider the typical mail.xml file first.
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<textview
android:layout_width= "Fill_parent"
Wrap_content "
android:text=" @string/hello "/>
</LinearLayout>
In Main.xnl, you can see that the root node element is <linearlayout>, and it contains a <TextView> element. This <LinearLayout> element controls the order of views that it contains.
Each view and viewgroup have some common attributes, some examples:
Property |
Describe |
Layout_width |
Specify the width of view or ViewGroup |
Layout_height |
Specify the height of view or ViewGroup |
Layout_margintop |
Specify extra space above view or ViewGroup |
Layout_marginbottom |
Specify additional space below view or ViewGroup |
Layout_marginleft |
Specify extra space to the left of view or ViewGroup |
Layout_marginright |
Specify additional space on the right side of view or ViewGroup |
Layout_gravity |
Specify the placement of child views in view or ViewGroup |
Layout_weight |
Specify additional space dimensions assigned to view or ViewGroup |
Layout_x |
Specify the X coordinate of view or viewgroup |
Layout_y |
Specify the y-coordinate of the view or ViewGroup |
For example, the,<textview> element uses the Fill_parent constant, so the width fills the width of the entire layout. Its height is set to the Wrap_content constant, which means that its height will be the same as the layout it is in. If you don't want to <TextView> occupy the entire screen, you can set the Layoutt_width element to wrap_content, like this:
<textview
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text = "@string/hello"/>
The code above sets the width of the textview to the same length as the text it contains.
Please see the following layout:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<textview
android:layout_width= "100DP"
android:layout_height= "WRAP_" Content "
android:text=" @string/hello "/>
<button
android:layout_width=" 160DP "
android: layout_height= "Wrap_content"
android:text= "button"/>
</LinearLayout>
Here, the width of the TextView and button is set to a fixed value.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/