android 佈局中 layout_gravity、gravity、orientation、layout_weight【轉】

線性佈局中,有 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個參數

<? xml version = "1.0" encoding = "utf-8" ?>

< 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 >

 

說明:在上面的例子中,根佈局是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 如下,看看效果

 

<? xml version = "1.0" encoding = "utf-8" ?>

< 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" />

</ LinearLayout >

 

通過改變android:gravity 參數的值可以看到實際效果。


參數 android:orientation= " horizontal " 決定了每個子元素各佔一列,如果

參數 android:orientation= " vertical "  則每個子元素各佔一行,也就是從上到下排列了。


對於 LinearLayout 佈局的子元素,給每個子元素加上參數 android:layout_weight

看看效果

<? xml version = "1.0" encoding = "utf-8" ?>

< 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" />

</ LinearLayout >

 

 

 

Text 爲ONE 的權重爲1 ,但明顯佔的寬度比TWO 的小,百思不得其解,後來得知,如果把TextView 的參數android:layout_width "wrap_content" 全部修改爲 android:layout_width "fill_parent", 則 ok ,代碼如下


<? xml version = "1.0" encoding = "utf-8" ?>

< 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" />

</ LinearLayout >

 

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