Android TutorialsNo Comments

ll
1361 views.

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.

All children of a LinearLayout are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A LinearLayout respects margins between children and the gravity(right, center, or left alignment) of each child.

Layout Weight

xEqually weighted children
To create a linear layout in which each child uses the same amount of space on the screen, set theandroid:layout_height of each view to “0dp” (for a vertical layout) or theandroid:layout_width of each view to “0dp” (for a horizontal layout). Then set the android:layout_weight of each view to “1”.
LinearLayout also supports assigning a weight to individual children with the android:layout_weightattribute. 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, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.
For example, 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 will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

Example

ll1

LinearLayout.LayoutParams

XML attributes

android:gravity
Gravity for the view associated with these LayoutParams.
android:layout_weight
Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.

android:layout_gravity
Standard gravity constant that a child supplies to its parent. Defines how the child view should be positioned, on both the X and Y axes, within its enclosing layout.

top
Push object to the top of its container, not changing its size.

bottom
Push object to the bottom of its container, not changing its size.

left 
Push object to the left of its container, not changing its size.

right
Push object to the right of its container, not changing its size.

center_vertical
Place object in the vertical center of its container, not changing its size.

fill_vertical 
Grow the vertical size of the object if needed so it completely fills its container.

center_horizontal 
Place object in the horizontal center of its container, not changing its size.

fill_horizontal
Grow the horizontal size of the object if needed so it completely fills its container.
center

 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.

fill  
Grow the horizontal and vertical size of the object if needed so it completely fills its container.

clip_vertical
Additional option that can be set to have the top and/or bottom edges of the child clipped to its container’s bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.

clip_horizontal 
Additional option that can be set to have the left and/or right edges of the child clipped to its container’s bounds. The clip will be based on the horizontal gravity:a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.

start 
Push object to the beginning of its container, not changing its size.

end
Push object to the end of its container, not changing its size.

 

Be the first to post a comment.

Add a comment