android layout_weight的理解

android中layout_weight的理解

  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.

  重點有兩個

    1. layout_weight表示LinearLayout中額外空間的劃分(可能擴大應用layout_weight前的大小也可能縮小)。
    2. 按比例(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與delta:
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與delta:

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 + (2 * (-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,所以Button1的寬度爲60+100/2=110,Button2的寬度爲40+100/2=90。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章