Android:Layout_weight的深刻理解
最近寫(xiě)Demo,突然發(fā)現(xiàn)了Layout_weight這個(gè)屬性,發(fā)現(xiàn)網(wǎng)上有很多關(guān)于這個(gè)屬性的有意思的討論,可是找了好多資料都沒(méi)有找到一個(gè)能夠說(shuō)的清楚的,于是自己結(jié)合網(wǎng)上資料研究了一下,終于迎刃而解,寫(xiě)出來(lái)和大家分享。
首先看一下Layout_weight屬性的作用:它是用來(lái)分配屬于空間的一個(gè)屬性,你可以設(shè)置他的權(quán)重。很多人不知道剩余空間是個(gè)什么概念,下面我先來(lái)說(shuō)說(shuō)剩余空間。
看下面代碼:
- <?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"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left"
- android:text="one"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_weight="1.0"
- android:text="two"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="right"
- android:text="three"/>
- </LinearLayout>
運(yùn)行結(jié)果是:
看上面代碼:只有Button2使用了Layout_weight屬性,并賦值為了1,而B(niǎo)utton1和Button3沒(méi)有設(shè)置Layout_weight這個(gè)屬性,根據(jù)API,可知,他們默認(rèn)是0
下面我就來(lái)講,Layout_weight這個(gè)屬性的真正的意思:Android系統(tǒng)先按照你設(shè)置的3個(gè)Button高度Layout_height值wrap_content,給你分配好他們3個(gè)的高度,
然后會(huì)把剩下來(lái)的屏幕空間全部賦給Button2,因?yàn)橹挥兴臋?quán)重值是1,這也是為什么Button2占了那么大的一塊空間。
有了以上的理解我們就可以對(duì)網(wǎng)上關(guān)于Layout_weight這個(gè)屬性更讓人費(fèi)解的效果有一個(gè)清晰的認(rèn)識(shí)了。
我們來(lái)看這段代碼:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:background="#ff0000"
- android:layout_width="**"
- android:layout_height="wrap_content"
- android:text="1"
- android:textColor="@android:color/white"
- android:layout_weight="1"/>
- <TextView
- android:background="#cccccc"
- android:layout_width="**"
- android:layout_height="wrap_content"
- android:text="2"
- android:textColor="@android:color/black"
- android:layout_weight="2" />
- <TextView
- android:background="#ddaacc"
- android:layout_width="**"
- android:layout_height="wrap_content"
- android:text="3"
- android:textColor="@android:color/black"
- android:layout_weight="3" />
- </LinearLayout>
三個(gè)文本框的都是 layout_width=“wrap_content ”時(shí),會(huì)得到以下效果
按照上面的理解,系統(tǒng)先給3個(gè)TextView分配他們的寬度值wrap_content(寬度足以包含他們的內(nèi)容1,2,3即可),然后會(huì)把剩下來(lái)的屏幕空間按照1:2:3的比列分配給3個(gè)textview,所以就出現(xiàn)了上面的圖像。
而當(dāng)layout_width=“fill_parent”時(shí),如果分別給三個(gè)TextView設(shè)置他們的Layout_weight為1、2、2的話,就會(huì)出現(xiàn)下面的效果:
你會(huì)發(fā)現(xiàn)1的權(quán)重小,反而分的多了,這是為什么呢???網(wǎng)上很多人說(shuō)是當(dāng)layout_width=“fill_parent”時(shí),weighth值越小權(quán)重越大,優(yōu)先級(jí)越高,就好像在背口訣
一樣,其實(shí)他們并沒(méi)有真正理解這個(gè)問(wèn)題,真正的原因是Layout_width="fill_parent"的原因造成的。依照上面理解我們來(lái)分析:
系統(tǒng)先給3個(gè)textview分配他們所要的寬度f(wàn)ill_parent,也就是說(shuō)每一都是填滿他的父控件,這里就死屏幕的寬度
那么這時(shí)候的剩余空間=1個(gè)parent_width-3個(gè)parent_width=-2個(gè)parent_width (parent_width指的是屏幕寬度 )
那么***個(gè)TextView的實(shí)際所占寬度應(yīng)該=fill_parent的寬度,即parent_width + 他所占剩余空間的權(quán)重比列1/5 * 剩余空間大?。?2 parent_width)=3/5parent_width
同理第二個(gè)TextView的實(shí)際所占寬度=parent_width + 2/5*(-2parent_width)=1/5parent_width;
第三個(gè)TextView的實(shí)際所占寬度=parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列顯示了。
這樣你也就會(huì)明白為什么當(dāng)你把三個(gè)Layout_weight設(shè)置為1、2、3的話,會(huì)出現(xiàn)下面的效果了:
第三個(gè)直接不顯示了,為什么呢?一起來(lái)按上面方法算一下吧:
系統(tǒng)先給3個(gè)textview分配他們所要的寬度f(wàn)ill_parent,也就是說(shuō)每一都是填滿他的父控件,這里就死屏幕的寬度
那么這時(shí)候的剩余空間=1個(gè)parent_width-3個(gè)parent_width=-2個(gè)parent_width (parent_width指的是屏幕寬度 )
那么***個(gè)TextView的實(shí)際所占寬度應(yīng)該=fill_parent的寬度,即parent_width + 他所占剩余空間的權(quán)重比列1/6 * 剩余空間大?。?2 parent_width)=2/3parent_width
同理第二個(gè)TextView的實(shí)際所占寬度=parent_width + 2/6*(-2parent_width)=1/3parent_width;
第三個(gè)TextView的實(shí)際所占寬度=parent_width + 3/6*(-2parent_width)=0parent_width;所以就是2:1:0的比列顯示了。第三個(gè)就直接沒(méi)有空間了。