转载地址:Android中的Layout_weight详解
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=75447&fromuid=38037

布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android "
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"
/>
</LinearLayout>
出现的布局是: button1占了2/3,button2占了1/3。
Android中的Layout_weight详解_第1张图片
但是如果将布局文件中的button的属性 android:layout_width="fill_parent"改为 android:layout_width="wrap_content"那么出现的结果为: button1占了1/3,button2占了2/3。
Android中的Layout_weight详解_第2张图片
出现这样的结局是什么意思呢?下面是人家的详解:转载过来:

*******转载的解释*********
linearLayout中包含有weight的child时,linearLayout会measure两次:
设屏幕宽度为X
第一次:button1的measuredWidth为X,button2也为X (因为用了weight,所以linearLayout每次measure child时不考虑前一个已经占用的大小),total_width为2X
第二次:计算delta=x-total_width=-x,然后会将button1的宽度设为
x+delta*1/3=0.66x, button2的宽度为 x+delta*2/3=0.33x
那我现在对这句话重新概括一下:“因为设置了button1的权重最小,所以它占用的布局优先级就越高”,也许在Android里面布局并没有优先级之说,我这里只是为了说明问题,自己定义的,所以朋友们不要拍砖。
那首先分析一下當layout_width屬性設置為fill_parent的時候,即充滿父佈局,當然意思是這個控件要根據weight的設置盡可能的大,因此,依上例而論,button1的weight設為1,button2的weight設置為2.即button的優先級最高,因此,要填充父佈局就要button1先來填充,盡可能的大,那這個盡可能又是多少呢,這就要綜合layout裡其他控件的weight值了,然後做一下運算,button1佔據2/3,button2佔據1/3.你也可以把button2設置為一個非常大的數,比如2000,此時在Graphical Layout模式下可以看到button1填充滿了整個寬度,而看不到button2的影子,事實上button2還是存在的,你把鼠標指向button1的後面就可以看到一個長長的豎條,那個就是button2,已經非常非常小了。 因此,在layout_width設置為fill_parent的時候,weight所代表的是你的控件要優先盡可能的大。

接著是當layout_weight設置為wrap_content的時候,即適應內容的寬度,意思是這個控件要盡可能的小,只要能把內容顯示出來就可以了,同樣的,如果把button1和button2的layout_weight設置為wrap_content後,button1的weight為1,button2的weight為2.那麼button1要優先盡可能的小,而button2也要盡可能的小,只是優先級不一樣,因為設置了weight,所以這兩個控件總的寬度要填滿父佈局的寬度,所以就又要計算每個控件所佔據的大小,此時,button1的優先級較高,共有兩份,一份1/3,一份2/3,button1要盡可能的小,那button1當然要選1/3,因此,我們看到的效果反而是button2佔據的較大。這裡要說的是如果把權值同樣做如下設置:button1為1,button2為2000,那button1是不是就要佔據1/2000的空間呢?這麼理解就錯了,剛才說了, 要盡可能的小,但這個小是有一個限度的,那就是wrap_content,就是還要是內容完完整整的顯示出來,同樣的,盡可能的大也是有一個限度的,那就是父佈局的寬度。因此,在layout_width設置為wrap_content的時候,weight所代表的是你的控件要優先盡可能的大。
所以,要對weight做了解,要深深的理解下面兩句話:
在layout_width設置為fill_parent的時候,layout_weight所代表的是你的控件要優先盡可能的大,但這個大是有限度的,即fill_parent.
在layout_width設置為wrap_content的時候,layout_weight所代表的是你的控件要優先盡可能的小,但這個小是有限度的,即wrap_content.

layout_height 同 layout_width.



最后贴几张图出来:
1. layout_width="fill_parent", button1的weight=1,button2的weight=2;

Android中的Layout_weight详解_第3张图片


2.layout_width="fill_parent", button1的weight=1,button2的weight=2000;
Android中的Layout_weight详解_第4张图片


3.layout_width="wrap_content", button1的weight=1,button2的weight=2;



4.layout_width="wrap_content", button1的weight=1,button2的weight=2000;
Android中的Layout_weight详解_第5张图片
*******转载的解释********* 转载地址:http://hi.baidu.com/ljlkings/blog/item/fa2a59803f839a82f603a6b2.html?timeStamp=1305190390481

更多相关文章

  1. android的五大布局
  2. android中控件的大部分属性解释
  3. 系出名门Android(6) - 控件(View)之DatePicker, TimePicker, Tog
  4. 第七章 Android 常见的UI基础控件 (三)
  5. Android随笔之布局属性简单用法
  6. Android的常用基本控件ImageView、ListView
  7. Android学习―LinearLayout布局中实现左右对齐
  8. android相对布局 相对对齐

随机推荐

  1. LayoutAnimation
  2. 在Android上执行Java程序
  3. Android创建自定义键盘
  4. zxing项目源码解读(2.3.0版本,Android部分)
  5. Android Service小实践
  6. Android使用Intent实现页面跳转
  7. Java和Javascript互调的例子 ---------(An
  8. 如何让Android屏幕只能上下翻转
  9. Android:控件GridView的使用实例
  10. android获取gps坐标