LinearLayout organizes a view into one row or one column. Child views can be arranged vertically or horizontally. To learn how LinearLayout works, first consider the typical mail. xml file.
- <? 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: orientation = "vertical">
- <TextView
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/hello"/>
- </LinearLayout>
In main. xnl, the root node element is <LinearLayout>, which contains a <TextView> element. The <LinearLayout> element controls the order of views it contains.
Each View and ViewGroup have some common attributes. For example:
Attribute |
Description |
Layout_width |
Specify the width of the View or ViewGroup. |
Layout_height |
Height of a View or ViewGroup |
Layout_marginTop |
Extra space above the View or ViewGroup |
Layout_marginBottom |
Specify additional space under the View or ViewGroup |
Layout_marginLeft |
Extra space on the left side of the View or ViewGroup |
Layout_marginRight |
Extra space on the right of the View or ViewGroup |
Layout_gravity |
Specify the position of the child views in the View or ViewGroup. |
Layout_weight |
Extra space size assigned to View or ViewGroup |
Layout_x |
X coordinate of the View or ViewGroup |
Layout_y |
Y coordinate of View or ViewGroup |
For example, the <TextView> element uses the fill_parent constant, so the width fills the width of the layout where the entire element is located. Its height is set to the constant wrap_content, which means that its height will be the same as its layout. If you do not want <TextView> to occupy the entire screen, you can set the layoutt_width element to wrap_content, just like this:
- <TextView
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/hello"/> <span style = "font-family: Arial, Helvetica, sans-serif;">
- </Span> www.2cto.com
The preceding Code sets the TextView width to the same length as the text contained in the TextView.
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: orientation = "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 TextView and Button width are set to a fixed value.
From manoel's column