2024年4月1日发(作者:)

从头学Android之Android布局管理:LinerLayout

线性布局

分类: 从头学Android系列 2011-10-12 17:175628人阅读评论(2)收藏举报

androidlayoutcontainersencodingxmlimport

LinerLayout线性布局:

这种布局方式是指在这个里面的控件元素显线性,我们可以通过setOrientation(int

orientation)来指定线性布局的显示方式,其值有:HORIZONTAL(0)、VERTICAL(1)。默认

为HORIZONTAL。与之相关的我们也可以在布局文件中通过android:orientation来指定。

同理,其值也有:horizontal、vertical

LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置

来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失,不能完

全显示。因此垂直方式排列时,每一行只会有一个 widget或者是container,而不管他们有

多宽,而水平方式排列是将会只有一个行高(高度为最高子控件的高度加上边框高度)。

LinearLayout保持其所包含的 widget或者是container之间的间隔以及互相对齐(相对一个

控件的右对齐、中间对齐或者左对齐)。

关于layout_weight:

LinearLayout还支持为其包含的widget或者是container指定填充权值。允许其包含的

widget或者是container可以填充屏幕上的剩余空间。剩余的空间会按这些widgets或者是

containers指定的权值比例分配屏幕。默认的 weight值为0,表示按照widgets或者是

containers实际大小来显示,若高于0的值,则将 Container剩余可用空间分割,分割大小

具体取决于每一个widget或者是 container的layout_weight及该权值在所有widgets或者

是containers中的比例。例如,如果有三个文本框,前两个文本框的取值一个为2,一个为

1,显示第三个文本框后剩余的空间的2/3给权值为2的,1/3大小给权值为1的。而第三个文

本框不会放大,按实际大小来显示。也就是权值越大,重要度越大,显示时所占的剩余空间

越大。

示例1:

[html]view plaincopyprint?

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">







 android:layout_height="wrap_content" android:layout_weight="1"



 android:text="1111" />











 android:layout_height="wrap_content" android:layout_weight="2"



 android:text="2222" />











 android:layout_height="wrap_content" android:text="3333" />



