Android中的Linear Layout

今天在看android开发的文章,看到线性布局的时候,看不明白里面的layout_weight属性,所以干脆翻译出来研究,以下是原文的翻译

 -------------------------------------------------------------------------------------------------------

LinearLayout aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute. All children 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.

线性布局已一个方向来排列所有的孩子-垂直的或水平的,这取决于你如何去定义orientation属性。所有的孩子一个接一个的靠在一起,因此一个垂直列表将在每一行上只有一个孩子,无论他们多宽,并一个水平列表将只有一个行高(取决于最高孩子的高度,不足的添加空白)。一个线性布局主要用于维护空白边在孩子和孩子的gravity(不知道这个词怎么翻译,不过应该是和布局有关系)。

LinearLayout also supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer 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 boxes and two of them declare a weight of 1, while the other is given no weight (0), the third text box 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 boxes are measured. If the third box 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.
线性布局也支持分配一个权重到独立的孩子。这个属性分配一个“重要的”值给视图,并允许它扩展填充剩余的空间在父视图上。孩子视图可以指定一个整型权重值,然后在这个视图组里的剩余空间将以孩子权重的百分比来分配。默认权重是0.例如,如果有3个文本框,其中2个的权重是1,当剩下那个被赋予0时,第3个文本框因为没有权重而不会扩展并且以内容区域为基准。另外2个会平均分配剩余的空间。如果第3个文本框被赋予了权重2,那么就意味着它被声明为比其他两个“更重要”。因此它将占据所有剩余空间的一半,剩余空间是布局完1,2两个文本框后的空间。

Tip: To create a proportionate size layout on the screen, create a container view group object with the layout_width and layout_height attributes set to fill_parent; assign the children height or width to 0 (zero); then assign relative weight values to each child, depending on what proportion of the screen each should have.
提示:创建一个按百分比大小布局的屏幕,创建一个容器视图组以fill_parent来设置layout_width和layout_height属性;设置孩子的高度和宽度为0;然后分配相关的权重值到每一个孩子,权重值取决于孩子要占据屏幕的百分比。


Within a horizontal LinearLayout, items are aligned by the position of their text base line (the first line of the first list element — topmost or leftmost — is considered the reference line). This is so that people scanning elements in a form shouldn't have to jump up and down to read element text in neighboring elements. This can be turned off by setting android:baselineAligned="false" in the layout XML.
在一个水平线性布局中,成员被排列到他们文本的基线位置(列表的第一个元素的第一行-顶端或左边-这个被认为参考线)。这是为了让人们在一个窗体上扫描元素时,不至于上下跳动的在相邻的元素中读取文本。这个属性可以被关闭,通过在layout的XML中设置android:baselineAligned="false"。
posted @ 2010-03-04 00:06  moonz-wu  阅读(3659)  评论(0编辑  收藏  举报