Android基礎教程之五大布局對象------FrameLayout,LinearLayout,AbsoluteLayout,RelativeLayout,TableLayout

你們好,咱們這一節講一下Android對用五大布局對象,它們分別是FrameLayout(框架佈局:不知道是否是這麼翻譯的),LinearLayout (線性佈局),AbsoluteLayout(絕對佈局),RelativeLayout(相對佈局),TableLayout(表格佈局).

FrameLayout:

FrameLayout是最簡單的一個佈局對象。它被定製爲你屏幕上的一個空白備用區域,以後你能夠在其中填充一個單一對象 — 好比,一張你要發佈的圖片。全部的子元素將會固定在屏幕的左上角;你不能爲FrameLayout中的一個子元素指定一個位置。後一個子元素將會直接在前一個子元素之上進行覆蓋填充,把它們部份或所有擋住(除非後一個子元素是透明的)。

咱們看一下效果圖:

其中Main.xml 代碼以下:
android

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     >

  6. <!-- 咱們在這裏加了一個Button按鈕 -->
  7. <Button
  8.     android:text="button"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11. />
  12. <TextView
  13.     android:text="textview"
  14.     android:textColor="#0000ff"
  15.     android:layout_width="wrap_content"
  16.     android:layout_height="wrap_content"
  17. />
  18. </FrameLayout>
複製代碼
LinearLayout:

LinearLayout以你爲它設置的垂直或水平的屬性值,來排列全部的子元素。全部的子元素都被堆放在其它元素以後,所以一個垂直列表的每一行只會有一個元素,而無論他們有多寬,而一個水平列表將會只有一個行高(高度爲最高子元素的高度加上邊框高度)。LinearLayout保持子元素之間的間隔以及互相對齊(相對一個元素的右對齊、中間對齊或者左對齊)。

LinearLayout還支持爲單獨的子元素指定weight 。好處就是容許子元素能夠填充屏幕上的剩餘空間。這也避免了在一個大屏幕中,一串小對象擠成一堆的狀況,而是容許他們放大填充空白。子元素指定一個weight 值,剩餘的空間就會按這些子元素指定的weight 比例分配給這些子元素。默認的 weight 值爲0。例如,若是有三個文本框,其中兩個指定了weight 值爲1,那麼,這兩個文本框將等比例地放大,並填滿剩餘的空間,而第三個文本框不會放大。

咱們看一下效果圖:

其中Main.xm l代碼以下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent">
  6.   <LinearLayout
  7.     android:orientation="vertical"
  8.     android:layout_width="fill_parent"
  9.     android:layout_height="fill_parent"
  10.     android:layout_weight="2">
  11.     <TextView
  12.         android:text="Welcome to Mr Wei's blog"
  13.         android:textSize="15pt"
  14.         android:layout_width="fill_parent"
  15.         android:layout_height="wrap_content"
  16.     />
  17.     </LinearLayout>
  18.     <LinearLayout
  19.     android:orientation="horizontal"
  20.     android:layout_width="fill_parent"
  21.     android:layout_height="fill_parent"
  22.     android:layout_weight="1">
  23.   
  24.     <TextView
  25.         android:text="red"

  26.         android:gravity="center_horizontal" //這裏字水平居中
  27.         android:background="#aa0000"
  28.         android:layout_width="wrap_content"
  29.         android:layout_height="fill_parent"
  30.         android:layout_weight="1"/>
  31.     <TextView
  32.         android:text="green"
  33.         android:gravity="center_horizontal "
  34.         android:background="#00aa00"
  35.         android:layout_width="wrap_content"
  36.         android:layout_height="fill_parent"
  37.         android:layout_weight="1"/>  
  38.     </LinearLayout>
  39. </LinearLayout>
複製代碼

AbsoluteLayout:
AbsoluteLayout 可讓子元素指定準確的x/y座標值,並顯示在屏幕上。(0, 0)爲左上角,當向下或向右移動時,座標值將變大。AbsoluteLayout 沒有頁邊框,容許元素之間互相重疊(儘管不推薦)。咱們一般不推薦使用 AbsoluteLayout ,除非你有正當理由要使用它,由於它使界面代碼太過剛性,以致於在不一樣的設備上可能不能很好地工做。框架

 

咱們看一下效果圖:

其中Main.xm l代碼以下:
佈局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <EditText
  8.     android:text="Welcome to Mr Wei's blog"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11. />
  12. <Button
  13.     android:layout_x="250px" //設置按鈕的X座標
  14.     android:layout_y="40px" //設置按鈕的Y座標
  15.     android:layout_width="70px" //設置按鈕的寬度
  16.     android:layout_height="wrap_content"
  17.     android:text="Button"
  18. />
  19. </AbsoluteLayout>
複製代碼
RelativeLayout:

RelativeLayout 容許子元素指定他們相對於其它元素或父元素的位置(經過ID 指定)。所以,你能夠以右對齊,或上下,或置於屏幕中央的形式來排列兩個元素。元素按順序排列,所以若是第一個元素在屏幕的中央,那麼相對於這個元素的其它元素將以屏幕中央的相對位置來排列。若是使用XML 來指定這個 layout ,在你定義它以前,被關聯的元素必須定義。

讓咱們看一下效果圖:

 

其中Main.xml 代碼以下: ui

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent">
  5.     <TextView
  6.         android:id="@+id/label"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:text="Welcome to Mr Wei's blog:"/>
  10.     <EditText
  11.         android:id="@+id/entry"
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="wrap_content"
  14.         android:layout_below="@id/label"/>
  15.     <Button
  16.         android:id="@+id/ok"
  17.         android:layout_width="wrap_content"
  18.         android:layout_height="wrap_content"
  19.         android:layout_below="@id/entry"
  20.         android:layout_alignParentRight="true"
  21.         android:layout_marginLeft="10dip"
  22.         android:text="OK" />
  23.     <Button
  24.         android:layout_width="wrap_content"
  25.         android:layout_height="wrap_content"
  26.         android:layout_toLeftOf="@id/ok"
  27.         android:layout_alignTop="@id/ok"
  28.         android:text="Cancel" />
  29. </RelativeLayout>
複製代碼
TableLayout:

TableLayout 將子元素的位置分配到行或列中。一個TableLayout 由許多的TableRow 組成,每一個TableRow 都會定義一個 row (事實上,你能夠定義其它的子對象,這在下面會解釋到)。TableLayout 容器不會顯示row 、cloumns 或cell 的邊框線。每一個 row 擁有0個或多個的cell ;每一個cell 擁有一個View 對象。表格由列和行組成許多的單元格。表格容許單元格爲空。單元格不能跨列,這與HTML 中的不同。

下面讓咱們看一下效果圖:

其中Main.xml 代碼以下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"
  4.     android:stretchColumns="1">
  5.     <TableRow>
  6.         <TextView android:layout_column="1" android:text="Open..." />
  7.         <TextView android:text="Ctrl-O" android:gravity="right" />
  8.     </TableRow>
  9.     <TableRow>
  10.         <TextView android:layout_column="1" android:text="Save..." />
  11.         <TextView android:text="Ctrl-S" android:gravity="right" />
  12.     </TableRow>
  13.     <View android:layout_height="2dip" android:background="#FF909090" /> //這裏是上圖中的分隔線
  14.     <TableRow>
  15.         <TextView android:text="X" />
  16.         <TextView android:text="Export..." />
  17.         <TextView android:text="Ctrl-E" android:gravity="right " />
  18.     </TableRow>
  19.     <View android:layout_height="2dip" android:background="#FF909090" />
  20.     <TableRow>
  21.         <TextView android:layout_column="1" android:text="Quit"
  22.             android:padding="3dip" />
  23.     </TableRow>
  24. </TableLayout>
複製代碼
以上就是Android五大布局對象。
相關文章
相關標籤/搜索