2025-05-24

 SDK中的解釋
Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
  重點有兩個,一個是layout_weight表示LinearLayout中額外空間的劃分(可能擴展應用layout_weight前的大小也可能壓縮),另一個是按比例.
  以下說的都以 android:orientation="horizontal" 為例
  看瞭一下源碼,雖說不太懂,但瞭解瞭下大概意思,按照自己的理解總結一下,直接寫一下簡化的代碼吧(下面的代碼是LinearLayout源文件中一部分的精簡,變量名稱含義可能不準確,為敘述方便暫作此解釋):
//Either expand children with weight to take up available space or
// shrink them if they extend beyond our current bounds
int delta = widthSize – mTotalLength;
if (delta != 0 && totalWeight > 0.0f) {
    float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight;
    for (int i = 0; i < count; ++i) {
        final View child = getVirtualChildAt(i);

        if (child == null || child.getVisibility() == View.GONE) {
            continue;
        }
       
        final LinearLayout.LayoutParams lp =
                (LinearLayout.LayoutParams) child.getLayoutParams();

        float childExtra = lp.weight;
        if (childExtra > 0) {
            int share = (int) (childExtra * delta / weightSum);
       weightSum -= childExtra;
        delta  -= share;
            int childWidth = child.getMeasuredWidth() + share;
            if (childWidth < 0) {
                childWidth = 0;
            }
        }
    }
}
變量含義
widthSize:     LinearLayout的寬度
mTotalLength:  所有子View的寬度的和(還沒用考慮layout_weight)
totalWeight:   所有子View的layout_weight的和
mWeihtSUm:    LinearLayout的android:weightSum屬性
過程分析:
首先計算出額外空間(可以為負)如果額外空間不為0並且有子View的layout_weight不為0的話按layout_weight分配額外空間:
int delta = widthSize – mTotalLength;
if (delta != 0 && totalWeight > 0.0f) {
  …
}
如果LinearLayout設置瞭weightSum則覆蓋子View的layout_weight的和:
float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight;
然後遍歷LinearLayout的子元素,如果不為null且Visibility不為GONE的話,取得它的LayoutParams,如果它的layout_weight大於0,根據weightSum與它的weight計算出分配給它的額外空間
if (childExtra > 0) {
    int share = (int) (childExtra * delta / weightSum);
   weightSum -= childExtra;
   delta -= share;

    int childWidth = child.getMeasuredWidth() + share;
    if (childWidth < 0) {
        childWidth = 0;
    }
}

網上有解釋說layout_weight表示重要程度,表示劃分額外空間的優先級,通過代碼可以知道這種觀點是錯誤的.layout_weight表示劃分的比例,至於當View的layout_width為fill_parent時layout_weight比例相反的問題按我的理解可以作以下解釋:
比如說如下XML:
<?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:background="#00ff00"
    android:weightSum="0"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/imageViewLoginState"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="1" >
    </Button>

    <Button
        android:id="@+id/imageViewLoginState1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="2" >
    </Button>

    <Button
        android:id="@+id/imageViewLoginState2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:text="3" >
    </Button>

</LinearLayout>

按一般理解,3個Button的比例應該為1:1:2,但實際情況是這樣的:

 

按我的理解,系統是這樣設置按鈕的大小的,變量名按前面代碼的意義:
假設Container即LinearLayout的寬度為PARENT_WIDTH
三個按鈕的寬度都是FILL_PARENT,所以在應用layout_width之前,三個按鈕的寬度都為PARENT_WIDTH
所以額外空間 delta = PARENT_WIDTH – 3 * PARENT_WIDTH = -2 * PARENT
因為LinearLayout沒有設置android:weightSum(默認為0,設置為0就當沒設置吧),所以 mWeightSum = 1 + 1 +2 =4
所以:
  第一個按鈕的寬度為PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum) = PARENT_WIDTH + (1 * (-2 * PARENT_WIDTH) /4) = 1 /2 *PARENT_WIDTH
    weightSum -= childExtra;(=3)
    delta  -= share;(=-3/2 * PARENT_WIDTH)
  第二個按鈕的寬度為PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum) = PARENT_WIDTH + (1 * (-3 / 2 * PARENT_WIDTH) /3) = 1 /2 *PARENT_WIDTH
    weightSum -= childExtra;(=2)
    delta  -= share;(=-PARENT_WIDTH)
  第三個按鈕的寬度為PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum) = PARENT_WIDTH + (2 * (- PARENT_WIDTH) /2) = 0
所以最終的而已就是前兩個按鈕平分LinearLayout,第三個按鈕消失瞭.
 
  大致過程是這樣,但不全對,比如如果上例中LinearLayout的weightSum設置為2的話,前兩個按鈕的寬度為0,但當計算第三個按鈕的寬度是mWeightSum = 0,但layout_weight * delta / mWeightSum無法計算,不知道系統怎麼處理的,在我的能力之外瞭,weightSum為2時的效果圖:

 weightSum為3時的效果圖:

 

 

SDK中說明的是,layout_weight表示額外空間怎麼劃分,要註意額外2字,要有額外的空間才可以將按比例將其分配給設置瞭layout_weight的子View,所以,如果LinearLayout設置為WRAP_CONTENT的話是沒有額外的空間的,layout_weight就沒有用處,所隻要layout_width不設置為WRAP_CONTENT就行,也可以設置為具體的值,如果值太小的話,額外空間為負,可能壓縮子控件,使其大小比XML文件中定義的小,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:background="#00ff00"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="1" >
    </Button>

    <Button
        android:id="@+id/button2"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="2" >
    </Button>

    <Button
        android:id="@+id/button3"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:text="3" >
    </Button>

</LinearLayout>
額外空間 delta = 100- 3 * 60 = -80
mWeightSum = 1 + 1 +2 =4
所以:
  第一個按鈕的寬度為60+ share = 60 + (layout_weight * delta / mWeightSum) = 60 + (1 * (-80) /4) = 40
    weightSum -= childExtra;(=3)
    delta  -= share;(=-60)
  第二個按鈕的寬度為60 + share = 60 + (layout_weight * delta / mWeightSum) = 60 + (1 * (-60) /3) = 40
    weightSum -= childExtra;(=2)
    delta  -= share;(=-40)
  第三個按鈕的寬度為60 + share = 60 + (layout_weight * delta / mWeightSum) = 60 + (2 * (-40) /2) = 20
效果圖:

 

 

以下代碼也說明瞭layout_weight表示額外空間的分配:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:background="#00ff00"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="1" >
    </Button>

    <Button
        android:id="@+id/button2"
        android:layout_width="40dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="2" >
    </Button>

  
</LinearLayout>
額外空間為100,所以Button2的寬度為60+100/2=100,Button2的寬度為40+100/2=90

 

作者:AngelDevil
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *