坑爹的android:Layout_weight屬性

天調(diào)整界面的時候遇到了一個需求,在一個fill_parent的水平的LinearLayout中有兩個RelativeLayout,這兩個RelativeLayout之間的寬度要求為2:3,我第一時間的反應(yīng)就是使用android:Layout_weight屬性,第一個設(shè)置為2,第二個設(shè)置為3,結(jié)果坑爹的反過來了。于是我不信邪的把android:Layout_weight的值反了一下,結(jié)果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"/>

結(jié)果:

然后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"/>

結(jié)果:

從上面兩個結(jié)果可以看出當(dāng)LinearLayout的子控件具備android:layout_weight屬性時,結(jié)果的表現(xiàn)會因為子控件的android:layout_width屬性是match_parent或者wrap_content而出現(xiàn)截然不同的結(jié)果:

match_parent時,android:layout_weight越小,占的空間越大;fill_parent則相反。

但是這里有一點比較奇怪的是android:layout_width="match_parent"時兩個控件的比例和設(shè)置的一致,但是android:layout_width="wrap_content"時兩個控件的比例就不是設(shè)置的比例了,這個有人知道是為什么嗎??



http://www.oschina.net/question/272860_81867

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容