Android中layout_weight的基本使用

LinearLayout佈局中的layout_weight,將會通過LinearLayout設置水平android:orientation="horizontal",或垂直android:orientation="vertical",對其中的子控件進行比例分配佔有空間,以下用水平爲例。

1.將子控件寬度設置爲:android:layout_width="0dp"

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/contact_name1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright"
            android:text="1"
            android:textSize="50sp" />

        <TextView
            android:id="@+id/contact_name2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@android:color/holo_red_light"
            android:text="2"
            android:textSize="50sp"
            />

        <TextView
            android:id="@+id/contact_name3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@android:color/holo_green_dark"
            android:text="3"
            android:textSize="50sp" />
    </LinearLayout>
效果圖如下:


android:layout_width="0dp"爲情況下,直接根據android:layout_weight=""值的比例進行分配即可,及時內容超過控件大小也不影響它們之間佔有的比例。


2.  將子控件寬度設置爲: android:layout_width="wrap_content"

    此時會有兩種情況,一種是,內容不多,沒超過控件大小,是按比例顯示的,如圖左所示效果,另一種,當內容超過控件大小時,會擠壓其他相鄰控件,實現不了我們要想的效果,但是你採用1中android:layout_width="0dp"就可以解決此類問題,如圖右。

                         


3.將子控件寬度設置爲:android:layout_width="match_parent"

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/contact_name1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright"
            android:text="1"
            android:textSize="50sp" />

        <TextView
            android:id="@+id/contact_name2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@android:color/holo_red_light"
            android:text="2"
            android:textSize="50sp"
            />

        <TextView
            android:id="@+id/contact_name3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@android:color/holo_green_dark"
            android:text="3"
            android:textSize="50sp" />
    </LinearLayout>

看效果


這是怎麼計算的呢?以上權重layout_weight的值分別爲1,2,2;設置它們的父控件match_parent和手機屏幕一樣大。

1>假設手機屏幕寬度爲480,它們三個子控件都爲layout_width="match_parent",總共佔用3×480;

2>父控件剩餘的空間寬度w爲手機屏幕480-子控件總佔有寬度3×480,結果爲w= -2×480(其值可以爲負值);

3>然後獲得每個子控件在剩餘空間寬度,分別爲w×1/5,w×2/5,w×2/5;

4>最後每個子控件佔有的空間寬度爲:480+w×1/5 :480+w×2/5 :480+w×2/5(w爲負值);

    計算結果爲3:1:1

再改下:把權重layout_weight分別設置爲1,2,3;

效果圖如下:


計算同時1>,2>不變,只是改變了比例;

3>子控件分別佔有剩餘空間寬度爲w×1/6,w×1/3,w×1/2。

4>480+w×1/6 :480+w×1/3 :480+w×1/2=2:1:0

所以顯示效果,第三個子控件爲0,沒有空間佔有,不顯示,第一個和第二個子控件按比例2:1顯示。

總結,其實就是 父控件寬度+父控件剩餘寬度×子控件權重比例。


4.當你只有一個子控件時,顯示佔有父控件一半時,可以這樣設置weightSum屬性,來達到效果:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" 
        android:weightSum="2">

        <TextView
            android:id="@+id/contact_name1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright"
            android:text="1"
            android:textSize="50sp" />
       
    </LinearLayout>

效果圖如下:


總結:其實子控件顯示的寬度與自己的父控件寬度的比例,是通過layout_weight/weightSum值來設置的,從而可以設置不同的比例,來進行顯示不同的寬度。


5. android:baselineAligned的設置,baselineAligned默認設置爲true,當設置爲false時, 佈局文件和它的內容的基準線不對齊,但可以看效果,此屬性是幹什麼的:

當設置爲false時:


設置爲true或不設置時(默認爲true)


看圖差別,就知道它的效果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章