android中layout_weight的理解

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.