在前面的文章中 http://www.cnblogs.com/ai-developers/p/android_linearlayout.html

我们看到了布局中有这样一个属性:

  layout_weight="1"

它的作用是什么。

我们先来做一个假设:有一个界面,要求元素在垂直方向上所占的空间一样,你会怎样做呢?

有人会说:将元素的属性layout_height设置相同的值就可以了啊。确实这样是可以的。

但是如果我有一个要求:这些元素所占的总空间要刚好匹配Activity的大小,不能有溢出。

那你会不会用尺子先量一下Activity的高度,再将值平均分配给各个元素?

当然这样做很傻。只是开个玩笑。

先来看一下下面代码的运行效果

 1 <LinearLayout
 2     android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     android:orientation="vertical"
 5     xmlns:android="http://schemas.android.com/apk/res/android">
 6     
 7     <Button 
 8         android:layout_width="150dp"
 9         android:layout_height="150dp"
10         android:layout_gravity="center"
11         android:text="发送"/>
12     
13     <Button 
14         android:layout_width="150dp"
15         android:layout_height="150dp"
16         android:layout_gravity="center"
17         android:text="发送"/>
18     
19     <Button 
20         android:layout_width="150dp"
21         android:layout_height="150dp"
22         android:layout_gravity="center"
23         android:text="发送"/>
24     
25     <Button 
26         android:layout_width="150dp"
27         android:layout_height="150dp"
28         android:layout_gravity="center"
29         android:text="发送"/>
30      <Button 
31         android:layout_width="150dp"
32         android:layout_height="150dp"
33         android:layout_gravity="center"
34         android:text="发送"/>
35 </LinearLayout>
View Code

相关文章: