注:本文轉載自blog.csdn.net/p106786860/article/details/10194083,此文解決了編程過程中對weight的疑惑,轉載以利於日後學習之用,感謝原文作者!
在開發的過程中,爲了佈局更好的適配各種各樣的屏幕,會經常使用android:layout_weight屬性,按比例分配屏幕的空間。在很多資料和書籍中解釋說,系統根據layout_weight比例分配佔據空間的大小。但是這個解釋在實際開發過程中,往往給我們帶來許多困惑。
現在我們來看看具體場景如下:我們需要將三個TextView按照1:2:3的橫向的比例顯示。於是就有了如下代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview_weight_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:gravity="center"
android:text="Weight_1" />
<TextView
android:id="@+id/textview_weight_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#FFA500"
android:gravity="center"
android:text="Weight_2" />
<TextView
android:id="@+id/textview_weight_3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="#F0FFFF"
android:gravity="center"
android:text="Weight_3" />
</LinearLayout>
觀察結果顯示如下(我們分別修改三個TextView的laytou_width屬性):
1.layout_width:wrap_content
2.layout_width:fill_parent
3.layout_width:"0dp"
在查閱官方文檔:Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight。意思是,子視圖可以指定一個權重值,然後在這個視圖組中的所有剩餘空間根據它們聲明的權重比例分配。
查閱相關的各方面資料,綜合總結,layout_weight比例分配的原則如下:
第一步,根據layout_width/layout_height(根據方向而定)的屬性分配視圖的空間
第二步,計算視圖組剩餘的空間大小,並按照layout_weight比例分配給子視圖
第三步,子視圖實際空間=layout_width/layout_height分配空間+layout_weight比例分配空間
根據如上步驟我們分別解釋上面三種情況結果的原因:
爲了更好的解釋,這裏我們聲明兩個常量content_width(內容所需佔據的空間大小)和screen_width(屏幕的寬度空間大小)。
1.layout_width:wrap_content
第一步,根據layot_width:wrap_content分配三個TextView內容填充的空間,三個TextView的layout_width空間分別爲:
layout_width1=content_width
layout_width2=content_width
layout_width3=content_width
第二步,視圖組線性佈局剩餘的空間大小=screen_width- 3 * content_width;那麼三個TextView的layout_weight比例分配空間分別爲:
layout_weight1=(screen_width-3*content_width)/6
layout_weight2=(screen_width-3*content_width)/3
layout_weight3=(screen_width-3*content_width)/2
第三步,根據計算公式,子視圖實際空間=layout_width/layout_height分配空間+layout_weight比例分配空間 ,三個TextView的空間分別爲:
textview1= content_width + (screen_width-3*content_width)/6
textview2= content_width + (screen_width-3*content_width)/3
textview3= content_width + (screen_width-3*content_width)/2
那麼textview1:textview2:textview3=(screen_width-2*content_width):2*(screen_width-2*content_width):3*screen_width。顯然,該比率是隨着content_width和screen_width而變化的,不能滿足我們的需求。
2.layout_width:fill_parent
第一步,三個TextView的layout_width空間分別爲:
layout_width1=screen_width
layout_width2=screen_width
layout_width3=screen_width
第二步,三個TextView的layout_weight比例分配空間分別爲:
layout_weight1=(screen_width-3*screen_width)/6=-screen_width/3
layout_weight2=(screen_width-3*screen_width)/3=-2*screen_width/3
layout_weight3=(screen_width-3*screen_width)/2=-screen_width
第三步,根據計算公式,子視圖實際空間=layout_width/layout_height分配空間+layout_weight比例分配空間 ,三個TextView的空間分別爲:
textview1= screen_width - screen_width/3 = 2 * screen_width/3
textview2= screen_width - 2 * screen_width/3 = screen _width/3
textview3= screen_width - screen_width = 0
那麼textview1:textview2:textview3=2:1:0,符合我們觀察到的現象!但不滿足需求。
3.layout_width:"0dp"
第一步,三個TextView的layout_width空間分別爲:
layout_width1=0dp
layout_width2=0dp
layout_width3=0dp
第二步,三個TextView的layout_weight比例分配空間分別爲:
layout_weight1=(screen_width-0dp)/6=screen_width/6
layout_weight2=(screen_width-0dp)/3=screen_width/3
layout_weight3=(screen_width-0dp)/2=screen_width/2
第三步,根據計算公式,子視圖實際空間=layout_width/layout_height分配空間+layout_weight比例分配空間 ,三個TextView的空間分別爲:
textview1= 0dp+screen_width/6=screen_width/6
textview2= 0dp+screen_width/3=screen_width/3
textview3= 0dp+screen_width/2=screen_width/2
那麼textview1:textview2:textview3=1:2:3,符合我們觀察到的結果,完全滿足我們的需求!!!
綜上的分析和總結,建議在開發的過程當中,如果想根據自己的意願,按照比例分配子視圖佔據的空間,將你分配的方向的layout_width或layout_height屬性設置爲”0dp"。