Android中layout_weight的属性理解

https://www.zybuluo.com/zzudhj/note/102067

 

在Android开发过程中,在编写布局文件经常会用layout_weight属性:从字面意思上看权重、比值、按比例。。。 
通过例子来看看layout_weight的用法 
example1: 
实现布局 
该布局有三部分组成,title、edit、bottom,其中,为了满足edit可以填充父窗口,可以为EditText添加layou_weight属性。通过Dump view UI hierarchy for Automatorfen 分析,得到结构如图: 
此处输入图片的描述 
通过代码实现

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="48dp"
  8. android:gravity="center"
  9. android:text="标题1" />
  10. <EditText
  11. android:layout_width="match_parent"
  12. android:layout_height="0dp"
  13. android:layout_weight="1" />
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content">
  17. <Button
  18. android:layout_width="0dp"
  19. android:layout_height="48dp"
  20. android:layout_weight="1"
  21. android:text="save" />
  22. <Button
  23. android:layout_width="0dp"
  24. android:layout_height="48dp"
  25. android:layout_weight="1"
  26. android:text="cancel" />
  27. </LinearLayout>
  28. </LinearLayout>

有了初步的认识,就来看看经常会遇到情景 
example2: 
此处输入图片的描述

 
  1. <TextView
  2. android:layout_width="0dp"
  3. android:layout_height="match_parent"
  4. android:text="textview1"
  5. android:background="#ee0000"
  6. android:layout_weight="1"/>
  7. <TextView
  8. android:layout_width="0dp"
  9. android:layout_height="match_parent"
  10. android:text="textview2"
  11. android:background="#eeee00"
  12. android:layout_weight="2"/>
  13. <TextView
  14. android:layout_width="0dp"
  15. android:layout_height="match_parent"
  16. android:text="textview3"
  17. android:background="#ee0ee0"
  18. android:layout_weight="2"/>

example3: 
此处输入图片的描述

 
  1. <TextView
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:text="textview1"
  5. android:background="#ee0000"
  6. android:layout_weight="1"/>
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:text="textview2"
  11. android:background="#eeee00"
  12. android:layout_weight="2"/>
  13. <TextView
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:text="textview3"
  17. android:background="#ee0ee0"
  18. android:layout_weight="2"/>

其中,example2与example3区别

 
  1. <TextView
  2. android:layout_width="xxx"
  3. ...
  4. />

分析可以知道layout_weight表示:控件长度= 原来长度(Length) + 屏幕剩余(SLength)* weight_i,其中屏幕长度为L 
example2中: 
Length = 0, 
SLength= L, 
textview1.weight = 1/5 
textview1.length = 1/5L; 
同理 textview2.length = 2/5L 
textview3.length = 2/5L 
example3中: 
Length= L, 
SLength = -2L, 
textview1.length = L + (- 2/5 L) = 3/5L 
textview2.length = L + (- 4/5 L) = 1/5L 
textview3.length = L + (- 4/5 L) = 1/5L

ps: 
textview1.weight =1 
textview2.weight =2 
textview3.weight =3 
如果这样设置layout_weight的值,当 android:layout_width="match_parent"的情景下, 
你会发现 
textview1.length = L + (- 2/6 L) = 2/3L 
textview2.length = L + (- 4/6 L) = 1/3L 
textview3 不见啦啦啦

posted @ 2015-09-28 19:22  zzxd  阅读(283)  评论(0编辑  收藏  举报