layout_weight属性

对自己现在所了解的layout_weight属性进行记录,不求全面,只求正确! 

layout_weight意为"权重",我的理解就是给组件设置一个显示大小的比例。 
layout_weight设置一个值,会出现两种情况。 
第一种:当组件的“layout_width”属性为“fill_parent”时,值越小,组件越大。 
第二种:当组件的“layout_width”属性为“wrap_content”时,值越大,组件越大。 

第一种情况: 
Xml代码  收藏代码
  1. <LinearLayout   
  2.     android:layout_weight="1"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="horizontal"  
  6.     android:layout_marginLeft="10dp"  
  7.     android:layout_marginRight="10dp"  
  8. >  
  9.     <Button   
  10.         android:id="@+id/btn_save"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_weight="1"  
  14.         android:text="保存"  
  15.     />  
  16.       
  17.     <Button   
  18.         android:id="@+id/btn_return"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_weight="4"  
  22.         android:text="返回"  
  23.     />  
  24. </LinearLayout>  

在这里"保存"按钮的Layout_weight=1,"返回"按钮的Layout_weight=4,layout_width="fill_parent"时, 运行效果为: 


第二种情况: 
Xml代码  收藏代码
  1. <LinearLayout   
  2.     android:layout_weight="1"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="horizontal"  
  6.     android:layout_marginLeft="10dp"  
  7.     android:layout_marginRight="10dp"  
  8. >  
  9.     <Button   
  10.         android:id="@+id/btn_save"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_weight="1"  
  14.         android:text="保存"  
  15.     />  
  16.       
  17.     <Button   
  18.         android:id="@+id/btn_return"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_weight="4"  
  22.         android:text="返回"  
  23.     />  
  24. </LinearLayout>  

在这里"保存"按钮的Layout_weight=1,"返回"按钮的Layout_weight=4,layout_width="wrap_content"时,运行效果为: 


以上为本人对已知情况的总结,如有不对或总结不足,望指教!
posted @ 2012-08-13 17:48  小鸟阿欢  阅读(258)  评论(0)    收藏  举报