Android Codingprogrammingtechnology

Android Coding: Linear Layout

A Linear Layout is a view group that aligns all children in a single direction. This means that all children of a Linear Layout are stacked one after the other. So, a vertical list will only have one child per row, no matter how wide they are. Similarly, a horizontal list will only be one row high (the height of the tallest child, plus padding). This view group respects margins between children and the gravity (right, center, or left alignment) of each child. You can specify the layout direction with the android: orientation attribute.

Layout_weight

Linear Layout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an “importance” value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value. Then, any remaining space in the view group is assigned to children in the proportion of their declared weight. The default weight is zero.

Equal distribution

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to “0dp” (for a vertical layout) or the android:layout_width of each view to “0dp” (for a horizontal layout). Then, set the android:layout_weight of each view to “1”.

Unequal distribution

You can also create linear layouts where the child elements use different amounts of space on the screen:

If there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight doesn’t grow. Instead, this third text field occupies only the area required by its content. The other two text fields, on the other hand, expand equally to fill the space remaining after all three fields are measured.

If there are three text fields and two of them declare a weight of 1, while the third field is then given a weight of 2 (instead of 0), then it’s now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

What's your reaction?

Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

Leave a reply

Your email address will not be published. Required fields are marked *

Next Article:

0 %