1. 程式人生 > >layout_weight的兩種設定方法。

layout_weight的兩種設定方法。

Android的關於layout_weight的計算方式。
首先注意layout的作用方向取決於父框架orientation指定。

layout_wight的值在計算中的作用。
首先了解下weight屬性的意義:(這裡只考慮寬)規定本控制元件可繼續獲得多少父佈局所剩寬(L)。先看例子吧

要實現這樣的效果:左邊文字wrap_content,右邊文字wrap_content,中間輸入框獲取父佈局剩下的所有寬度,程式碼:

  <LinearLayout
android:id="@+id/holder" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="20dp" android:gravity="center_vertical" android:orientation="horizontal" >
<TextView android:layout_width="wrap_content"
android:layout_height="50dp" android:background="#ff0099" android:gravity="center_vertical" android:text="請輸入金額:" android:textSize="15sp" />
<EditText android:layout_width="wrap_content" android:layout_height
="50dp" android:layout_weight="1" android:background="#f2f2f2" android:hint="這裡可輸入" />
<TextView android:layout_width="wrap_content" android:layout_height="50dp" android:background="#44d4ff" android:gravity="center_vertical" android:padding="5dp" android:text="元" android:textSize="15sp" /> </LinearLayout>

父佈局寬度設為:L,預設權重weight = 0
每個控制元件的寬度設定過程可以理解為:

1,先按照每個控制元件的layout_width的值來設定控制元件寬度,這裡都是wrap_content ,這時都能顯示全本控制元件裡面的內容
2,計算剩餘寬度 S = L-W1-E-W2;(W1:左邊TextView的寬度,E:EditText的寬度,W2:右邊TextView的寬度)
3,把剩餘的寬度 S 按照weight值進行分配,由於兩個TextView的weight = 0,所以EditText能分配到的寬度為 S * 1 /( 0+1+0)=S,所以EditText能獲得所有的剩餘寬度。

方法一

要想這三個控制元件平分父佈局的寬度的話,可以這樣設定;

    <LinearLayout
        android:id="@+id/holder"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:background="#ff0099"
            android:gravity="center_vertical"
            android:layout_weight="1"
            android:text="請輸入金額:"
            android:textSize="15sp" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#f2f2f2"
            android:hint="這裡可輸入" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#44d4ff"
            android:gravity="center_vertical"
            android:padding="5dp"
            android:text="元"
            android:textSize="15sp" />
    </LinearLayout>

把每個控制元件的寬度都設定為0dp,這樣能保證剩餘寬度就是總寬度,再按照weight 分就能保證平分

最後分析下這樣的效果和對應的程式碼:

方法二

注意將layout_weight改為match_parent的使用。

 <LinearLayout
        android:id="@+id/holder"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#ff0099"
            android:gravity="center_vertical"
            android:layout_weight="1"
            android:text="請輸入金額:"
            android:textSize="15sp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="2"
            android:background="#f2f2f2"
            android:hint="這裡可輸入" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="2"
            android:background="#44d4ff"
            android:gravity="center_vertical"
            android:padding="5dp"
            android:text="元"
            android:textSize="15sp" />
    </LinearLayout>

1,按照控制元件的width屬性放置控制元件,每個控制元件的寬度都是 L,後兩個控制元件是被擠出螢幕的
2,計算父佈局剩餘寬度 S = L-(L+L+L)= -2L
3,給每個控制元件分配剩餘寬度,計算後左邊TextView寬度 = L+(-2L*1/(1+2+2))= 3L/5