Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android AbsoluteLayout 與 RelativeLayout 版面詳解

編輯:關於 Android 程式設計

本文介紹 Android 的兩種版面:AbsoluteLayout 絕對版面與 RelativeLayout 相對版面。

1、絕對版面 AbsoluteLayout

絕對版面又稱座標版面,可直接指定子元素的絕對位置,簡單直觀、一目了然;但由於手機螢幕尺寸差異大,使用絕對定位的適應性較差。

以下範例中的機器人圖片為 250×250,我們用 android:layout_x 與 android:layout_y 來指定子元素的橫縱座標:

 <?xml version=”1.0″ encoding=”utf-8″?> <AbsoluteLayout android:id=”@+id/AbsoluteLayout01″ android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”https://schemas.android.com/apk/res/android” android:background=”#fff”><ImageView android:src=”@drawable/android” android:layout_y=”40dip” android:layout_width=”wrap_content” android:layout_x=”35dip” android:id=”@+id/ImageView01″ android:layout_height=”wrap_content”> </ImageView> <TextView android:layout_height=”wrap_content” android:layout_width=”fill_parent” android:id=”@+id/TextView01″ android:text=”Android2.2 學習指南” android:textColor=”#0f0″ android:textSize=”28dip” android:layout_y=”330dip” android:layout_x=”35dip“> </TextView> <TextView android:layout_height=”wrap_content” android:layout_width=”fill_parent” android:id=”@+id/TextView02″ android:text=”圖文並茂,理論清晰,操作性強” android:textColor=”#333″ android:textSize=”18dip” android:layout_y=”365dip” android:layout_x=”35dip“> </TextView> </AbsoluteLayout>

在 WQVGA 模擬器下的顯示效果:

在 WVGA800 模擬器下的顯示效果:

Tip:在絕對定位中,若子元素未設定 layout_x 與 layout_y,其預設值為 0,也就是會像 FrameLayout 一樣出現在左上角。

2、相對版面 RelativeLayout

RelativeLayout 允許子元素指定相對於父元素或兄弟元素的位置,是實務上最常用的一種版面,靈活性高、屬性也多,但操作難度與屬性衝突的可能性也隨之提高,使用時要多做測試。

以下用相對版面重做一次上面的例子,先放一張圖片,另兩個文字相對於前一元素定位:

 <?xml version=”1.0″ encoding=”utf-8″?><RelativeLayout android:id=”@+id/RelativeLayout01″ android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:background=”#fff” xmlns:android=”https://schemas.android.com/apk/res/android”><ImageView android:id=”@+id/ImageView01″ android:src=”@drawable/android” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:layout_marginTop=”40dip” > </ImageView> <TextView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:id=”@+id/TextView01″ android:text=”Android2.2 學習指南” android:textColor=”#0f0″ android:textSize=”28dip” android:layout_below=”@id/ImageView01″ android:layout_centerHorizontal=”true” android:layout_marginTop=”10dip”> </TextView> <TextView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:id=”@+id/TextView02″ android:text=”圖文並茂,理論清晰,操作性強” android:textColor=”#333″ android:textSize=”18dip” android:layout_below=”@id/TextView01″ android:layout_centerHorizontal=”true” android:layout_marginTop=”5dip“> </TextView> </RelativeLayout>

WQVGA 模擬器下的顯示效果:

更大螢幕(WVGA800)模擬器上的顯示效果:

從圖中可見,介面效果基本保持一致,而不像絕對定位那樣縮在左上角;這種自動縮放的好處正來自以 dip 為單位的設定。

以下介紹 RelativeLayout 的一些重要屬性:

第一類:屬性值為 true 或 false

  1. android:layout_centerHorizontal 水平居中
  2. android:layout_centerVertical 垂直居中
  3. android:layout_centerInparent 相對於父元素完全居中
  4. android:layout_alignParentBottom 貼緊父元素的下邊緣
  5. android:layout_alignParentLeft 貼緊父元素的左邊緣
  6. android:layout_alignParentRight 貼緊父元素的右邊緣
  7. android:layout_alignParentTop 貼緊父元素的上邊緣
  8. android:layout_alignWithParentIfMissing 若對應的兄弟元素找不到,則以父元素作參照

第二類:屬性值必須為 id 的引用名「@id/id-name」

  1. android:layout_below 在某元素的下方
  2. android:layout_above 在某元素的下方(上方)
  3. android:layout_toLeftOf 在某元素的左邊
  4. android:layout_toRightOf 在某元素的右邊
  5. android:layout_alignTop 本元素上邊緣與某元素上邊緣對齊
  6. android:layout_alignLeft 本元素左邊緣與某元素左邊緣對齊
  7. android:layout_alignBottom 本元素下邊緣與某元素下邊緣對齊
  8. android:layout_alignRight 本元素右邊緣與某元素右邊緣對齊

第三類:屬性值為具體像素值,如 30dip、40px

  1. android:layout_marginBottom 離某元素底邊緣的距離
  2. android:layout_marginLeft 離某元素左邊緣的距離
  3. android:layout_marginRight 離某元素右邊緣的距離
  4. android:layout_marginTop 離某元素上邊緣的距離

我們把上面的例子再做一遍,這次加入更多屬性,大家可以親手試驗:

 <?xml version=”1.0″ encoding=”utf-8″?><RelativeLayout android:id=”@+id/RelativeLayout01″ android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:background=”#cfff” 色彩的設置是argb,第一個c是透明度 xmlns:android=”https://schemas.android.com/apk/res/android”><ImageView android:id=”@+id/ImageView01″ android:src=”@drawable/android” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_marginTop=”40dip” android:layout_centerHorizontal=”true”> </ImageView><TextView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:id=”@+id/TextView01″ android:text=”Android2.2 學習指南” android:textColor=”#0f0″ android:textSize=”28dip” android:layout_below=”@id/ImageView01″ android:layout_centerHorizontal=”true” android:layout_marginTop=”10dip”> </TextView><TextView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:id=”@+id/TextView02″ android:text=”圖文並茂,理論清晰,操作性強” android:textColor=”#333″ android:textSize=”18dip” android:layout_below=”@id/TextView01″ android:layout_centerHorizontal=”true” android:layout_marginTop=”5dip”> </TextView><TextView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:id=”@+id/TextView03″ android:text=”alignTop” android:textColor=”#333″ android:textSize=”18dip” android:layout_alignTop=”@id/ImageView01″ 和ImageView01上邊緣對齊 android:layout_centerHorizontal=”true”> </TextView><TextView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:id=”@+id/TextView04″ android:text=”alignLeft” android:textColor=”#333″ android:textSize=”18dip” android:layout_alignLeft=”@id/ImageView01″ android:layout_centerHorizontal=”true”> </TextView><TextView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:id=”@+id/TextView05″ android:text=”alignRight” android:textColor=”#333″ android:textSize=”18dip” android:layout_alignRight=”@id/ImageView01″ android:layout_centerHorizontal=”true”> </TextView><TextView android:layout_height=”wrap_content” android:layout_width=”wrap_content” android:id=”@+id/TextView06″ android:text=”alignBottom” android:textColor=”#333″ android:textSize=”18dip” android:layout_alignBottom=”@id/ImageView01″ android:layout_centerHorizontal=”true”> </TextView> </RelativeLayout>

以上就是對 Android AbsoluteLayout 與 RelativeLayout 的介紹,後續將繼續整理相關資料。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved