Linear Layout 布局技巧

来源:互联网 发布:淘宝店铺出租平台 编辑:程序博客网 时间:2024/04/25 20:42


技巧:要创建一个每个子View在屏幕上都有相同空间的线性布局,对于垂直布局,要把每个子View的android:layout_height属性值都设置为”0dp”,对于与水平布局,要把每个子View的android:layout_width的属性值都设置为”0dp”。然后把每个子View的android:layout_weight属性值都设置为”1”。


Layout Weight


LinearLayout 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 is 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.


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.

Must be one or more (separated by '|') of the following constant values.

ConstantValueDescriptiontop0x30Push object to the top of its container, not changing its size.bottom0x50Push object to the bottom of its container, not changing its size.left0x03Push object to the left of its container, not changing its size.right0x05Push object to the right of its container, not changing its size.center_vertical0x10Place object in the vertical center of its container, not changing its size.fill_vertical0x70Grow the vertical size of the object if needed so it completely fills its container.center_horizontal0x01Place object in the horizontal center of its container, not changing its size.fill_horizontal0x07Grow the horizontal size of the object if needed so it completely fills its container.center0x11Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.fill0x77Grow the horizontal and vertical size of the object if needed so it completely fills its container.clip_vertical0x80Additional 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_horizontal0x08Additional 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.start0x00800003Push object to the beginning of its container, not changing its size.end0x00800005Push object to the end of its container, not changing its size.

This corresponds to the global attribute resource symbol layout_gravity.



原创粉丝点击