相對佈局LinearLayout權重weight的用法

大家好,這是我第一次寫博客,有什麼不足的地方,請大家多多指教,我會虛心反思,每一次都能有所進步微笑。這次我給大家帶來的是LinearLayout佈局裏的有關於權重weight相關知識,以及是怎樣在LinearLayout中如何正確的使用。


首先什麼都不說,先上這次代碼運行成功後的圖片。


這張圖片的實現效果運用到的是LinearLayout中weight這個知識點,下面我爲大家分析怎麼使用weight權重。


第一種weight權重:

上半部分豎條的效果,三個大小一致的顏色不同的豎條,它使用LinearLayout把三個控件包裹起來,其中使用了android:orientation="horizontal" 屬性,表示佈局是水平排列的,

進而你就知道了,它是靠佈局的寬來進行恆等分割,也就是權重的意思,而layout_width=設置爲0dp,每個需要權重的佈局都要加上android:layout_weight="1"屬性。嗯,這樣就o了,放代碼給大家看看。


 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#22ff0000"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="300dp"
            android:layout_weight="1"
            android:background="#ff6600" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="300dp"
            android:layout_weight="1"
            android:background="#ffffcc" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="300dp"
            android:layout_weight="1"
            android:background="#ffff99" />
    </LinearLayout>


第二種weight權重:

下半部分橫條的效果,要有這個效果LinearLayout設置屬性爲orientation="vertical" ,其他屬性省略,垂直排列的,它是靠長來進行恆等分割的,而把 屬性設置爲android:layout_height="0dp",layout_width可以設置任意值,但不能爲0,再加上 android:layout_weight="1",就可以執行上圖效果了,。上代碼,給大夥瞧瞧.....


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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#ffff0000" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#33ff00" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#66ffff" />
       
       
    </LinearLayout>



謝謝大家不厭其煩的看完我第一次不完美的微博,不滿意請留下你的評論,我會有則改之無則加勉,下次有更好的微博分享給大家。







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