LinearLayout中的android:layout_weight属性的设置和使用

来源:互联网 发布:荣威rx5改装矩阵式大灯 编辑:程序博客网 时间:2024/05/21 07:52

第一种设置:

这个东西很常用,也会用,但是基本上只用了一种方法,如下:

  1. <textview android:id="@+id/textView1"
  2. android:layout_width="0dip"
  3. android:layout_height="30dip"
  4. android:layout_weight="1"
  5. android:background="@android:color/darker_gray"
  6. android:gravity="center"
  7. android:text="a"></textview>
  8.  
  9. <textview android:id="@+id/textView2"
  10. android:layout_width="0dip"
  11. android:layout_height="30dip"
  12. android:layout_weight="2"
  13. android:background="@android:color/holo_green_light"
  14. android:gravity="center"
  15. android:text="b"></textview>

这就是我们经常用的方法,分为两步:

  • 1.将要在一个方向上根据权重分布的按钮或者布局的宽度或者高度设置为0dip(如以上代码中的android:layout_width=”0dip”)
  • 2.根据当前的布局要求以及权重相应的设置weight(如以上代码中的android:layout_weight=”1″)

这样设置之后,显示出来的界面上,textView1在横向上就占了屏幕宽度的1/3,而textView2占了2/3.
weight_test1

第二种设置

这种方法不是很常用,先来看代码:

  1. <TextView
  2. android:id="@+id/textView1"
  3. android:layout_width="match_parent"
  4. android:layout_height="30dip"
  5. android:layout_weight="1"
  6. android:background="@android:color/darker_gray"
  7. android:gravity="center"
  8. android:text="a" />
  9.  
  10. <TextView
  11. android:id="@+id/textView2"
  12. android:layout_width="match_parent"
  13. android:layout_height="30dip"
  14. android:layout_weight="2"
  15. android:background="@android:color/holo_green_light"
  16. android:gravity="center"
  17. android:text="b" />
  18.  
  19. <TextView
  20. android:id="@+id/textView3"
  21. android:layout_width="match_parent"
  22. android:layout_height="30dip"
  23. android:layout_weight="3"
  24. android:background="@android:color/holo_orange_light"
  25. android:gravity="center"
  26. android:text="c" />

weight_test2
你以为他会使上面的样子么?你太天真了。

注意看

注意看他们的layout_width都设置的是什么,不是之前的0dip了,而是match_parent。
设置了match_parent之后,界面是这样子的。
weight_test3
对,就是这样子的,我没有放错图。。
真的没有。
然后来说说,a和b就究竟对c做了些什么。。
先来放张图,下班路上想明白的,感谢各路司机不杀之恩。(地铁上手画的,不要吐槽)
weight_test4
首先看最上面的屏幕,a,b,c都是match_parents,那么如果他们每一个都是单独放置的话,将会占满整个屏幕,a的应该占1/6,b的应该1/3,c 的应该1/2,但是很不凑巧,他们仨是一条船上的。
这样系统在算的时候就犯嘀咕了,你丫三个,每个人还都想独自占有我的宽度(= =||),我只有一个,怎么能分给你们三个呢?但是我还要都满足你们,那我欠你们两个,你们就都可以占满我的宽度了。
于是,屏幕的宽度欠了abc总过两个整的屏幕宽度。也就是-2。
这欠你们的两个屏幕宽度平分给你们吧。
a不乐意了,我虽然要整个宽度,但是我权重只是占了1个啊,为什么平分。于是1+1*(-2/6)=2/3.
b也不乐意了,我也是要了整个宽度,但是我权重也只是占了2个啊,为什么平分。于是1+2*(-2/6)=1/3.
c哭了。。
你看,我没骗你们。。

第三种设置

我们刚刚看了c的辛酸史,现在来看看b和c的辛酸史(c:小b,你也有今天→_→)
来看代码:

  1. <TextView
  2. android:id="@+id/textView1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="30dip"
  5. android:layout_weight="1"
  6. android:background="@android:color/darker_gray"
  7. android:gravity="center"
  8. android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
  9.  
  10. <TextView
  11. android:id="@+id/textView2"
  12. android:layout_width="wrap_content"
  13. android:layout_height="30dip"
  14. android:layout_weight="2"
  15. android:background="@android:color/holo_green_light"
  16. android:gravity="center"
  17. android:text="b" />
  18.  
  19. <TextView
  20. android:id="@+id/textView3"
  21. android:layout_width="wrap_content"
  22. android:layout_height="30dip"
  23. android:layout_weight="3"
  24. android:background="@android:color/holo_orange_light"
  25. android:gravity="center"
  26. android:text="c" />

这一次,将所有的android:layout_width都设置为wrap_content,weight不变。
界面是这样的。
weight_test5
a:我是wrap_content,先!让!我!把!内容!!显!示!完!

b和c哭了


原文:http://sumile.cn/archives/913.html

转载已注明(这本来就是我写的!)

0 0