天天看點

線性布局的權重weight使用詳解

[size=large]對線性布局中權重的了解[/size]

轉載請注明出處:[url]http://renyuan-1991.iteye.com/blog/2272200[/url]

今天突然想到自己對線性布局的了解,便想再此記錄下來。寫這篇部落格之前特意看了很多别人寫的線性布局的權重了解發現隻有一篇是正确的,其餘都是按“包裹内容時權重越大比例越大,比對父窗體時權重越大比例越小”,或者反比正比的什麼規律。這樣的了解是不對的,對于自己寫動态布局會産生很大的困擾,如果按這樣的了解,下面的情況就無法解釋了。

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="3"
            android:background="@color/yellow"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
           

按照上面的說法這段代碼顯示的樣式應該是3:2:1,可實際的情況是下圖中的第二個線性布局

[img]http://dl2.iteye.com/upload/attachment/0114/6657/69544fb2-c011-3200-b0c6-f8f9cc097811.png[/img]

上面代碼顯示的布局中黃色的textview根本沒有顯示,是以“包裹内容時權重越大比例越大,比對父窗體時權重越大比例越小”這種說法是不正确的。[color=orange]控件最終寬度 = 控件初始寬度+((螢幕寬度-控件寬度和)/(所有weight之和))[/color]*這裡是水準方向的是以用寬度舉例,豎直方向同理。

接下來我們看一下上圖中對應的代碼

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="2"
            android:layout_width="0dp"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="3"
            android:background="@color/yellow"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="3"
            android:background="@color/yellow"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="1"
            android:background="@color/yellow"
            android:layout_width="100dp"
            android:layout_height="match_parent" />
    </LinearLayout>
           

第一段代碼:控件寬度之和是0dp,是以螢幕剩餘量是1個螢幕寬度。weight之和是6。先把螢幕剩餘量分6份,第一個textview占一份第二個TextView占兩份第三個TextView占三份。最後它們顯示的寬度就是0+1/6,0+2/6,0+3/6。

第二段代碼:控件寬度之和是3個螢幕寬度,是以螢幕剩餘量是-2個螢幕寬度。weight之和是6。先把螢幕剩餘量分6份,第一個textview占一份第二個TextView占兩份第三個TextView占三份。最後它們顯示的寬度就是1+(1/6)*(-2),1+(2/6)*(-2),1+(3/6)*(-2)。是以第一個TextView和第二個TextView已經占滿了螢幕第三個TextView自然就顯示不出來了

第三段代碼:控件寬度之和是100dp,是以螢幕剩餘量是1個螢幕寬度減去100dp。weight之和是6。先把螢幕剩餘量分6份,第一個textview占一份第二個TextView占兩份第三個TextView占三份。最後它們顯示的寬度就是0+(1/6)*剩餘量,0+(2/6)*剩餘量,100+(3/6)*剩餘量

ok,到這兒就結束了....

希望愛好程式設計的小夥伴能加這個群,互相幫助,共同學習。群号: 141877583

繼續閱讀