本文档中内容
IN THIS DOCUMENT
- 布局权重
Layout Weight - 示例
Example
关键类
KEY CLASSES
线性布局
LinearLayout线性布局.布局参数
LinearLayout.LayoutParams
线性布局 是一个视图分组,它在单一方向上对齐所有的子视图,或者竖直方向或者水平方向。可以使用
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
属性来指定布局方向。
LinearLayoutandroid: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
平等加权子视图
Equally weighted children
要创建一个线布局,要在其内部,使每一个子视图使用相同数量的屏幕空间,那么设置每一个设图的android:layout_height 属性为 "0dp"(针对于竖向布局)或者
android:layout_width
属性为 "0dp"(针对于水平布局),然后,为每一个视图设置 android:layout_weight
属性值为 "1"。
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"
.
线性布局还支持通过属性
also supports assigning a weight to individual children with the android:layout_weight
给各个子视图赋一个权重值。这个属性按照应该在屏幕上占有多少空间来给一个视图赋一个权重值。更大的权重值将允许它扩展以填充父视图中剩余的空间。子视图可以指定一个权重值,这样一来所有余下的视图组空间都会分配给声明了权重的那部分子视图。默认值是 0 。
LinearLayoutandroid: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.
例如,如果有三个文本哉,并且其中两个声明了 1 的权重,而另一个没有给定权重,那么第三个没有权重的文本域不会增长,只会占用其内容必需的区域(待测试:如果未指定权重而指定了宽或高,那么内容必需的区域是指实际内容还是宽、高值?还是通过 onMeasured 方法获得该视图的内容必需的区域?)。其它两个域就会平等地填充在所有三个域测量后余下的空间。如果第三个字段随后给定了一个 2 的权重(而不是 0 了),那么它现在声明的比其它两个权重更重了,这样它获得总共余下空间的一半,而头两个等分余下的部分。
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
<?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>
有关线性布局每一个子视图可用的属性的详细描述,可以查看 LinearLayout.LayoutParams 。
For details about the attributes available to each child view of a LinearLayout
, seeLinearLayout.LayoutParams
.