<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      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_weight之前,三個按鈕的寬度都為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。

      posted @ 2012-04-08 16:44  AngelDevil  閱讀(31414)  評論(11)    收藏  舉報
      主站蜘蛛池模板: 亚洲av男人电影天堂热app| 98精品全国免费观看视频| 在线国产你懂的| 亚洲精品日韩精品久久| 国产精品视频全国免费观看| 国产在线乱子伦一区二区| 奇米四色7777中文字幕| 国产精品中文字幕第一区| 亚洲综合视频一区二区三区| 国产成人高清精品免费软件| 无码专区 人妻系列 在线| 日韩69永久免费视频| 日本一区二区a√成人片| 亚洲精品国模一区二区| 国产粉嫩高中无套进入| 亚洲精品综合网中文字幕| 国内少妇偷人精品免费| 亚洲精品国产综合麻豆久久99 | 亚洲精品久久婷婷丁香51| 日韩人妻无码精品无码中文字幕| 高清破外女出血AV毛片| 色窝窝免费播放视频在线| 中文幕无线码中文字夫妻| 亚洲日本中文字幕天天更新| 国产精品一区二区久久岳| 四虎网址| 给我中国免费播放片在线| 国产女人喷潮视频免费| 尤物yw193无码点击进入| 国产乱子伦一区二区三区视频播放| 黑森林福利视频导航| 久久亚洲精品亚洲人av| 亚洲欧美人成人让影院| 欧美日韩国产亚洲沙发| 国厂精品114福利电影免费| 色五月丁香六月欧美综合| 日韩淫片毛片视频免费看| 国产亚洲精品国产福APP| 性奴sm虐辱暴力视频网站| 国产精品伦人一久二久三久| 麻花传媒在线观看免费|