線性布局中,有 4 個及其重要的參數,直接決定元素的布局和位置,這四個參數是
android:layout_gravity ( 是本元素相對於父元素的重力方向 )
android:gravity (是本元素所有子項目的重力方向)
android:orientation (線性布局以列或行來顯示內部子項目)
android:layout_weight (線性布局內子項目對未佔用空間【水平或垂直】分配權重值,其值越小,權重越大。
前提是子項目 設定了 android:layout_width = "fill_parent" 屬性(水平方向)
或 android:layout_height = "fill_parent" 屬性(垂直方向)
如果某個子項目的 android:layout_width = "wrap_content"
或 android:layout_height =" wrap_content” ,
則 android:layout_weight 的設定值 對該方向上空間的分配剛好相反。
下面以一個簡單例子來說明這 4個參數:
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_height = "200dp" android:layout_width = "200dp" android:background = "#AABBCC" android:orientation= "horizontal" android:layout_gravity= "center" > < TextView android:text = "ONE" android:background = "#aa0000" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_margin = "1dp" /> < TextView android:text = "TWO" android:background = "#aa0000" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_margin = "1dp" />
說明:在上面的例子中,根布局是LinearLayout, 其包含有2 個TextView 視圖,為了對參數 android:layout_gravity有直觀的瞭解,對根布局 LinearLayout 特意加了 3 個參數
android:layout_height = "200dp"
android:layout_width = "200dp"
android:background = "#AABBCC"
為布局指定了固定的寬度和高度,以及背景顏色,上面的例子運行後效果如:
說明:對LinearLayout 中的參數android:layout_gravity 來說,其意義是指定本布局相對於父布局的重力方向,由於該布局的已經是根布局,其父布局是整個螢幕,那麼該參數設定的是相對於螢幕的位置,可以換不同的參數top"bottom|left|right 等等參數來實驗。
現在增加參數 android:gravity = "bottom|right" 完整 XML 如下,看看效果
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_height = "200dp" android:layout_width = "200dp" android:background = "#AABBCC" android:orientation="horizontal" android:layout_gravity= "center" android:gravity = "bottom|right " > < TextView android:text = "ONE" android:background = "#aa0000" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_margin = "1dp" /> < TextView android:text = "TWO" android:background = "#aa0000" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_margin = "1dp" />
vcq1vMrQp7n7oaM8YnI+Cjxicj4KPGJyPgqyzsr9IGFuZHJvaWQ6b3JpZW50YXRpb249IA==" horizontal " 決定了每個子項目各佔一列,如果
參數 android:orientation= " vertical " , 則每個子項目各佔一行,也就是從上到下排列了。
對於 LinearLayout 布局的子項目,給每個子項目加上參數 android:layout_weight
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_height = "200dp" android:layout_width = "200dp" android:background = "#AABBCC" android:layout_gravity = "center" android:gravity = "bottom|right" android:orientation = "horizontal" > < TextView android:text = "ONE" android:background = "#aa0000" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_margin = "1dp" android:layout_weight = "1" /> < TextView android:text = "TWO" android:background = "#aa0000" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_margin = "1dp" android:layout_weight = "2" />
Text 為ONE 的權重為1 ,但明顯占的寬度比TWO 的小,百思不得其解,後來得知,如果把TextView 的參數android:layout_width = "wrap_content" 全部修改為 android:layout_width = "fill_parent", 則 ok ,代碼如下
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_height = "200dp" android:layout_width = "200dp" android:background = "#AABBCC" android:layout_gravity = "center" android:gravity = "bottom|right" android:orientation = "horizontal" > < TextView android:text = "ONE" android:background = "#aa0000" android:layout_height = "wrap_content" android:layout_width = " fill_parent " android:layout_margin = "1dp" android:layout_weight = "1" /> < TextView android:text = "TWO" android:background = "#aa0000" android:layout_height = "wrap_content" android:layout_width = " fill_parent " android:layout_margin = "1dp" android:layout_weight = "2" />