大家好,我們這一節講一下Android對用五大布局對象,它們分別是FrameLayout(架構布局:不知道是不是這麼翻譯的),LinearLayout (線性布局),AbsoluteLayout(絕對布局),RelativeLayout(相對布局),TableLayout(表格版面配置).
FrameLayout:
FrameLayout是最簡單的一個布局對象。它被定製為你螢幕上的一個空白備用地區,之後你可以在其中填充一個單一對象 —
比如,一張你要發布的圖片。所有的子項目將會固定在螢幕的左上方;你不能為FrameLayout中的一個子項目指定一個位置。後一個子項目將會直接在前
一個子項目之上進行覆蓋填充,把它們部份或全部擋住(除非後一個子項目是透明的)。
我們看一下:
其中Main.xml
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 我們在這裡加了一個Button按鈕 -->
<Button
android:text="button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="textview"
android:textColor="#0000ff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
LinearLayout:
LinearLayout以你為它設定的垂直或水平的屬性值,來排列所有的子項目。所有的子項目都被堆放在其它元素之後,因此一個垂直列表的每一行只會有
一個元素,而不管他們有多寬,而一個水平列表將會只有一個行高(高度為最高子項目的高度加上邊框高度)。LinearLayout保持子項目之間的間隔以
及互相對齊(相對一個元素的靠右對齊、中間對齊或者靠左對齊)。
LinearLayout還支援為單獨的子項目指定weight
。好處就是允許子項目可以填充螢幕上的剩餘空間。這也避免了在一個大螢幕中,一串小對象擠
成一堆的情況,而是允許他們放大填充空白。子項目指定一個weight
值,剩餘的空間就會按這些子項目指定的weight
比例分配給這些子項目。預設的
weight
值為0。例如,如果有三個文字框,其中兩個指定了weight
值為1,那麼,這兩個文字框將等比例地放大,並填滿剩餘的空間,而第三個文字框
不會放大。
我們看一下:
其中Main.xm
l代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<TextView
android:text="Welcome to Mr Wei's blog"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
//這裡字水平置中
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal
"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
AbsoluteLayout:
AbsoluteLayout
可以讓子項目指定準確的x/y座標值,並顯示在螢幕上。(0,
0)為左上方,當向下或向右移動時,座標值將變大。AbsoluteLayout
沒有頁邊框,允許元素之間互相重疊(儘管不推薦)。我們通常不推薦使用
AbsoluteLayout
,除非你有正當理由要使用它,因為它使介面代碼太過剛性,以至於在不同的裝置上可能不能很好地工作。
我們看一下:
其中Main.xm
l代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:text="Welcome to Mr Wei's blog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_x="250px"
//設定按鈕的X座標
android:layout_y="40px"
//設定按鈕的Y座標
android:layout_width="70px"
//設定按鈕的寬度
android:layout_height="wrap_content"
android:text="Button"
/>
</AbsoluteLayout>
RelativeLayout:
RelativeLayout
允許子項目指定他們相對於其它元素或父元素的位置(通過ID
指定)。因此,你可以以靠右對齊,或上下,或置於螢幕中央的形式來
排列兩個元素。元素按順序排列,因此如果第一個元素在螢幕的中央,那麼相對於這個元素的其它元素將以螢幕中央的相對位置來排列。如果使用XML
來指定這個
layout
,在你定義它之前,被關聯的元素必須定義。
讓我們看一下:
其中Main.xml
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog:"/>
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
TableLayout:
TableLayout
將子項目的位置分配到行或列中。一個TableLayout
由許多的TableRow
組成,每個TableRow
都會定義一個
row
(事實上,你可以定義其它的子物件,這在下面會解釋到)。TableLayout
容器不會顯示row
、cloumns
或cell
的邊框線。每個
row
擁有0個或多個的cell
;每個cell
擁有一個View
對象。表格由列和行組成許多的儲存格。表格允許儲存格為空白。儲存格不能跨列,這與HTML
中的不一樣。
下面讓我們看一下:
其中Main.xml
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:layout_column="1" android:text="Open..." />
<TextView android:text="Ctrl-O" android:gravity="right"
/>
</TableRow>
<TableRow>
<TextView android:layout_column="1" android:text="Save..." />
<TextView android:text="Ctrl-S" android:gravity="right"
/>
</TableRow>
<View android:layout_height="2dip" android:background="#FF909090" />
//這裡是中的分隔線
<TableRow>
<TextView android:text="X" />
<TextView android:text="Export..." />
<TextView android:text="Ctrl-E" android:gravity="right
" />
</TableRow>
<View android:layout_height="2dip" android:background="#FF909090" />
<TableRow>
<TextView android:layout_column="1" android:text="Quit"
android:padding="3dip" />
</TableRow>
</TableLayout>
以上就是Android五大布局對象,這個工程好龐大,足足花了我一個多小時的時間,希望對大家有協助~大家多留言!
參考文獻:http://www.androidcn.net/wiki/index.php/Devel/ui/layout