轉自:http://www.cnblogs.com/fly3q/archive/2010/03/24/1693977.html layout_weight 用於給一個線性布局中的諸多視圖的重要度賦值。 所有的視圖都有一個layout_weight值,預設為零,意思是需要顯示 多大的視圖就佔據多大的螢幕空 間。若賦一個高於零的值,則將父視圖中的可用空間分割,分割大小具體取決於每一個視圖的layout_weight值以及該值在當前螢幕布局的整體 layout_weight值和在其它視圖螢幕布局的layout_weight值中所佔的比率而定。 舉個例子:比如說我們在 水平方向上有一個文字標籤和兩個文本編輯元素.該文字標籤並無指定layout_weight值,所以它將佔據需要提供的最少空間。 如果兩個文本編輯元素每一個的layout_weight值都設定為1,則兩者平分在父視圖布局剩餘的寬度(因為我們聲明這兩者的重要度相等)。如果兩個文本編輯元素其中第一個的layout_weight值設定為1,而第二個的設定為2, 則剩餘空間的三分之二分給第一個,三分之一分給第二個(數值越小,重要度越高)。 看例子: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="redwwwwwww" android:gravity="center_horizontal" android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="green" android:gravity="center_horizontal" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="2"/> <TextView android:text="blue" android:gravity="center_horizontal" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="3"/> <TextView android:text="yellow" android:gravity="center_horizontal" android:background="#aaaa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="4"/> </LinearLayout> </LinearLayout>