现在的位置: 首页 > 综合 > 正文

android:layout_weight属性的使用

2013年09月15日 ⁄ 综合 ⁄ 共 2127字 ⁄ 字号 评论关闭

http://blog.csdn.net/hustspy1990/article/details/6725298

 

这个属性用于控制控件的大小,当LinearLayout布局是垂直布局的时候,它就控制高度的大小;当LinearLayout布局是水平布局的时候,它就控制宽度的大小。

<?xml version="1.0" encoding="utf-8"?>     

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     android:orientation="vertical"   
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent">         
  4.     <TextView android:id="@+id/sample_1"  
  5.         android:background="@color/red" android:layout_weight="1"  
  6.         android:layout_width="fill_parent" android:layout_height="0dip"  />  
  7.     <TextView android:id="@+id/sample_2"  
  8.         android:background="@color/black" android:layout_weight="2"  
  9.         android:layout_width="fill_parent" android:layout_height="0dip"  />  
  10.     <TextView android:id="@+id/sample_3"  
  11.         android:background="@color/white" android:layout_weight="3"  
  12.         android:layout_width="fill_parent" android:layout_height="0dip"  />  
  13.     <TextView android:id="@+id/sample_4"  
  14.         android:background="@color/blue" android:layout_weight="4"  
  15.         android:layout_width="fill_parent" android:layout_height="0dip"  />  
  16. </LinearLayout>  


上面代码显示出来之后,sample_1:sample_2:sample_3:sample_4高度比=1:2:3:4。

很重要的一点是,高度属性要设定为0,绝对不能设定为fill_parent,不然就不会成比例了!这是在布局是垂直布局时候的情况,水平时,则定义宽度属性为0!

抱歉!评论已关闭.