android:layout_weight越大所佔比例越大和越大所佔比例越小的兩個例子,
摘要: 我的技術部落格經常被流氓網站惡意爬取轉載。請移步原文:http://www.cnblogs.com/hamhog/p/3907146.html,享受整齊的排版、有效連結、正確的代碼縮排、更好的閱讀體驗。
關於android:layout_weight到底是什麼含義,在網上“越大所佔比例越大”和“越大所佔比例越小”兩種說法都有。實際上這兩種情況也都有。
例1:0dp
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ff0000" android:layout_weight="1" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:background="#00ff00" android:layout_weight="2" android:text="2"/></LinearLayout>
例2:
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#ff0000" android:layout_weight="1" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00ff00" android:layout_weight="2" android:text="2"/></LinearLayout>
對兩種情況的解釋:
布局首先會給每個元素分配寬度,剩餘的空間再分配給每個元素。
在分配剩餘空間階段,比例按照layout_weight,也就是越大分得越大。
在情況一裡,固定寬度為0,只有第二階段;情況二裡,分配寬度能把所有空間分滿,剩餘的空間是負數,所以分配的越大,最終空間越小。
所以如果你要嚴格地讓它按照某種比例,可以試試定為0dp。此時各個元素的比例即為weight。
LinearLayout中的子項目屬性android:layout_weight有其他的特點哦
不知道你問的是什麼 我目前這個屬性的用法 比如一行有2個控制項,然後每個控制項
android:layout_weight=“1” 他們就平分這個地區
再比如 你有個TextView 下面一個ListView 為了讓listview下拉時不蓋掉上面的textview 只需給listview 設定android:layout_weight=“1” 就可以了
android編程android:layout_weight="1"報錯“Invalid layout param in a RelativeLayout: layout_weight
親,android:layout_weight只適用於線性布局LinearLayout,不適用於相對布局RelativeLayout啊