關於對Layout_weight的理解

    最近一段時間,有朋友常常問我關於android佈局文件裏Layout_weight究竟是什麼做用,以前我也被困擾,隨着知識的增加,也慢慢知道了原因,並將本身的理解分享以下: android

    layout_weight的權重不是指父控件的權重,而是指父控件的空間按子控件大小分配完後剩餘空間的權重。 佈局

    直接上代碼上圖了 spa

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#800FF8"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black"
            android:text="weight=1"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@android:color/white"
            android:text="weight=2"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@android:color/black"
            android:text="weight=3"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:background="@android:color/white"
            android:text="weight=4"
            android:textColor="@android:color/black" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black"
            android:text="weight=1"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@android:color/white"
            android:text="weight=2"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@android:color/black"
            android:text="weight=3"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:background="@android:color/white"
            android:text="weight=4"
            android:textColor="@android:color/black" />
    </LinearLayout>

</LinearLayout> 3d

運行結果: xml

    第二行視圖中weight=2超出屏幕,weight=3跑偏了,weight=4沒了。 開發

    其實android是這樣計算的(以佈局文件中第一行視圖爲例): it

    首先,android會給每一個TextView分配wrap_content寬度,當4個TextView寬度加起來小於屏幕總寬度的話,那一切都會按咱們想象的出現4個TextView逐漸增大;反之大於屏幕寬度則會出現佈局錯位(能夠經過如下方法計算錯位的位置); io

    而後,android將屏幕寬度減去4個TextView總寬度後獲得餘量(有可能爲負),再根據每一個TextView的weight分配餘量; 方法

    最後,將TextView的寬度加上對應從餘量中所分配的大小,就是實際要顯示的寬度。 im

    第二行視圖也可經過一樣的計算所得,發現Weight=2超過屏幕大小,weight=3跑偏了,weight=4計算爲負,顯示不出來。

    因此,開發過程當中,建議子控件用match_parent,而且爲每一個子控件分配一樣的權重,每一個子控件所包含的內容大小盡可能一致。

相關文章
相關標籤/搜索