android:layout_weight詳細分析介紹:
布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"
/>
</LinearLayout>
出現的布局是:button1占了2/3,button2占了1/3。
但是如果將布局文件中的button的屬性android:layout_width="fill_parent"改為android:layout_width="wrap_content"那么出現的結果為:button1占了1/3,button2占了2/3。
出現這樣的結局是什么意思呢?下面是詳細解釋:
linearLayout中包含有weight的child時,linearLayout會measure兩次:
設屏幕寬度為X
第一次:button1的measuredWidth為X,button2也為X (因為用了weight,所以linearLayout每次measure child時不考慮前一個已經占用的大小),total_width為2X
第二次:計算delta=x-total_width=-x,然后會將button1的寬度設為
x+delta*1/3=0.66x, button2的寬度為 x+delta*2/3=0.33x
那我現在對這句話重新概括一下:“因為設置了button1的權重最小,所以它占用的布局優先級就越高”,也就是說layout_weight越小,布局優先級越高。也許在Android里面布局並沒有優先級之說,我這里只是為了說明問題,自己定義的,所以朋友們不要拍磚。
那首先分析一下當layout_width屬性設置為fill_parent的時候,即充滿父布局,當然意思是這個控件要根據weight的設置盡可能的大,因此,依上例而論,button1的weight設為1,button2的weight設置為2.即button1的優先級最高,因此,要填充父布局就要button1先來填充,盡可能的大,那這個盡可能又是多少呢,這就要綜合layout里其它控件的weight值了,然后做一下運算,button1占據2/3,button2占據1/3.你也可以把button2設置為一個非常大的數,比如2000,此時在Graphical Layout模式下可以看到button1填充滿了整個寬度,而看不到button2的影子,事實上button2還是存在的,你把鼠標指向button1的后面就可以看到一個長長的豎條,那個就是button2,已經非常非常小了。因此,在layout_width設置為fill_parent的時候,weight所代表的是你的控件要優先盡可能的大。
接着是當layout_weight設置為wrap_content的時候,即適應內容的寬度,意思是這個控件要盡可能的小,只要能把內容顯示出來就可以了,同樣的,如果把button1和button2的layout_weight設置為wrap_content后,button1的weight為1,button2的weight為2.那么button1要優先盡可能的小,而button2也要盡可能的小,只是優先級不一樣,因為設置了weight,所以這兩個控件總的寬度要填滿父布局的寬度,所以就又要計算每個控件所占據的大小,此時,button1的優先級較高,共有兩份,一份1/3,一份2/3,button1要盡可能的小,那button1當然要選1/3,因此,我們看到的效果反而是button2占據的較大。這里要說的是如果把權值同樣做如下設置:button1為1,button2為2000,那button1是不是就要占據1/2000的空間呢?這么理解就錯了,剛才說了,要盡可能的小,但這個小是有一個限度的,那就是wrap_content,就是還要是內容完完整整的顯示出來,同樣的,盡可能的大也是有一個限度的,那就是父布局的寬度。因此,在layout_width設置為wrap_content的時候,weight所代表的是你的控件要優先盡可能的小。
所以,要對weight做了解,要深深的理解下面兩句話:
在layout_width設置為fill_parent的時候,layout_weight所代表的是你的控件要優先盡可能的大,但這個大是有限度的,即fill_parent.
在layout_width設置為wrap_content的時候,layout_weight所代表的是你的控件要優先盡可能的小,但這個小是有限度的,即wrap_content.
layout_height 同 layout_width.
實際上上面做的解釋主要是對fill_parent和wrap_content做的解釋,我們可以參考下面的2篇博文知道,上面的解釋是存在一定的問題的。
一般來說,我們使用layout_weight,我們可能會使用在各個控件或者布局(像LinearLayout),應用最廣泛的就是DV6300-T的主界面的布局,全部用的是layout_weight來控制的,這樣可以更好的適應不同的分辨率。
所以我們經常用layout_weight來設置布局,一般是設置不同的LinearLayout為不同的layout_weight來完成基本布局,還可以利用LinearLayout來占據空間都是沒有任何顯示,這樣的就是完全實現比重布局的LinearLayout,最后在各個LinearLayout里面又實現多個控件的layout_weight控制,同樣也可以使用只占控件無顯示的控件來達到分配布局的效果,DV6300-T就是明顯例子。
還需要注意的就是LinearLayout的orientation如果沒有設置,那么默認的就是horizontal水平方向。
按比例顯示LinearLayout內各個子控件,需設置android:layout_width="0dp",
如果為豎直方向的設置android:layout_height="0dp"。
在這種情況下某子個控件占用LinearLayout的比例為:本控件weight值 / LinearLayout內所有控件的weight值的和。
一般我們控制比重都是設置對應方向的高或寬為0dp來實現我們想的比重控制,而很少用fill_parent或wrap_content,不過我們來理解這2種設置的區別。
還有一句話需要理解就是: 權重(layout_weight)最小,所以它占用的布局優先級就越高。
還需要注意的是保存一致性控制:
如果我們要控制顯示比重,那么我們一般要求各個等級的元素的控制方式必須一致,比如多個button控件處於同一等級來控制比重布局,那么如果設置的是android:layout_width="0dp"這樣的方式來控制,就必須把所有的等級控件都用android:layout_width="0dp"來控制,而不能有的用android:layout_width="0dp",而還有的用android:layout_width=" fill_parent ",這樣就會出現混亂了。
下面還有一個例子:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00AAC0"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#CCdd00"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000FF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#FFAA00"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#CCdd00"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000FF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00FF00"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#CCdd00"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#cc00FF"
/>
</LinearLayout>
</LinearLayout>
----------------------------------------------------------------------------------------------------------------------------------------------------
原帖地址:http://geyubin.iteye.com/blog/1482198