LinearLayout is a View group. All the child views in the group are arranged in the same direction, vertical or horizontal. You can use the android: orentation attribute to specify the layout direction.
All subviews in LinearLayout are arranged in sequence. Therefore, each row in the vertical list has only one subview, regardless of the width of the row; the horizontal list only has one row (the row height is filled with the highest child View height ). LinearLayout focuses on the edges between sub-views and the alignment of each sub-View (right, center, or left ). The layout weight LinearLayout also uses the android: layout_weight attribute to assign a weight to a separate sub-View. This attribute assigns an importance value to a View, which specifies the size of the screen space occupied by the View. A large weight value allows the View to expand and fill the remaining space in its parent View. The sub-View can specify a weight value, and the remaining space in the View group will be allocated to the sub-View according to the proportion of the declared weight. The default weight is 0. for example, if there are three text fields, and two of them declare that the weight value is 1, and the other does not, this field without the weight value will not grow, it only occupies the region required by its content. After all three text fields are measured, the other two will divide the remaining space equally. If the weight of the third text field is set to 2 (instead of 0), it is more important than the other two, so it will get half of the total remaining space, the other two will divide the remaining space equally. Example: <? 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: paddingLeft = "16dp" android: paddingRight = "16dp" android: orientation = "vertical"> <EditText android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: hint = "@ string/to"/> <EditText android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: hint = "@ string/subject"/> <EditText android: layout_width = "fill_parent" android: layout_height = "0dp" android: layout_weight = "1" android: gravity = "top" android: hint = "@ string/message"/> <Button android: layout_width = "100dp" android: layout_height = "wrap_content" android: layout_gravity = "right" android: text = "@ string/send"/> </LinearLayout>
Tip: Create a linear layout with the same space on the screen for each sub-View. For vertical layout, set the android: layout_height attribute value of each sub-View to "0dp ", for horizontal layout, set the attribute value of android: layout_width of each sub-View to "0dp ". Then, set the android: layout_weight attribute value of each sub-View to "1 ".