天調整界面的時候遇到了一個需求,在一個fill_parent的水平的LinearLayout中有兩個RelativeLayout,這兩個RelativeLayout之間的寬度要求為2:3,我第一時間的反應就是使用android:Layout_weight屬性,第一個設置為2,第二個設置為3,結果坑爹的反過來了。于是我不信邪的把android:Layout_weight的值反了一下,結果oK。靠,雖然問題解決但是讓我很蛋疼的是這和我的理解相反。本著“科研”的精神,我自己動手做了如下的實驗,一個LinearLayout中放了了兩個TextView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="50dip"
android:background="#ffff0000"
android:text="TextView1"/>
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_weight="2"
android:layout_height="50dip"
android:background="#ff00ff00"
android:text="TextView2"/>
結果:
然后xml做一點修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="50dip"
android:background="#ffff0000"
android:text="TextView1"/>
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_weight="2"
android:layout_height="50dip"
android:background="#ff00ff00"
android:text="TextView2"/>
結果:
從上面兩個結果可以看出當LinearLayout的子控件具備android:layout_weight屬性時,結果的表現會因為子控件的android:layout_width屬性是match_parent或者wrap_content而出現截然不同的結果:
match_parent時,android:layout_weight越小,占的空間越大;fill_parent則相反。
但是這里有一點比較奇怪的是android:layout_width="match_parent"時兩個控件的比例和設置的一致,但是android:layout_width="wrap_content"時兩個控件的比例就不是設置的比例了,這個有人知道是為什么嗎??
http://www.oschina.net/question/272860_81867