Android(安卓)中layout_weight屬性詳解

來源:互聯網
上載者:User

當控制項本身layout_width設定為fill_parent的時候,layout_weight數值越小,所佔空間越大,但儘可能大是有限度的,即fill_parent.

當控制項本身layout_width設定為wrap_content的時候,layout_weight數值越小,所佔空間也越小,但這個小是有限度的,即wrap_content.

例子
看了一下源碼,雖說不太懂,但瞭解了下大概意思,按照自己的理解總結一下,直接寫一下簡化的代碼吧(下面的代碼是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;
    }
}


例子

 代碼如下 複製代碼


    <?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:orientation="horizontal" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:layout_weight="1">  
          <TextView  
              android:text="redwwwwwww" 
              android:gravity="center_horizontal" 
              android:background="#aa0000" 
              android:layout_width="wrap_content" 
              android:layout_height="fill_parent" 
              android:layout_weight="1"/>  
          <TextView  
              android:text="green" 
              android:gravity="center_horizontal" 
              android:background="#00aa00" 
              android:layout_width="wrap_content" 
              android:layout_height="fill_parent" 
              android:layout_weight="2"/>  
          <TextView  
              android:text="blue" 
              android:gravity="center_horizontal" 
              android:background="#0000aa" 
              android:layout_width="wrap_content" 
              android:layout_height="fill_parent" 
              android:layout_weight="3"/>  
          <TextView  
              android:text="yellow" 
              android:gravity="center_horizontal" 
              android:background="#aaaa00" 
              android:layout_width="wrap_content" 
              android:layout_height="fill_parent" 
              android:layout_weight="4"/>  
      </LinearLayout>  
    </LinearLayout> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.