Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

第六講:使用者介面 View(二)—— AbsoluteLayout 與 RelativeLayout 實戰

編輯:Android 開發實例

本講將介紹兩種常用的 Layout:AbsoluteLayout(絕對定位)與 RelativeLayout(相對定位),並透過同一個範例對照兩者的差異。點此下載:Android 學習指南第六講源碼。

3、絕對定位 AbsoluteLayout

AbsoluteLayout 又叫座標定位,可以直接指定子元件的絕對位置。這種方式簡單直接、直觀性強,但由於手機螢幕尺寸差異相當大,使用絕對定位時的適應性會比較差。

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout android:id="@+id/AbsoluteLayout01"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. xmlns:android="https://schemas.android.com/apk/res/android"
  6. android:background="#fff">
  7. <ImageView
  8. android:src="@drawable/android"
  9. android:layout_y="40dip"
  10. android:layout_width="wrap_content"
  11. android:layout_x="35dip"
  12. android:id="@+id/ImageView01"
  13. android:layout_height="wrap_content">
  14. </ImageView>
  15. <TextView
  16. android:layout_height="wrap_content"
  17. android:layout_width="fill_parent"
  18. android:id="@+id/TextView01"
  19. android:text="Android2.2 學習指南"
  20. android:textColor="#0f0"
  21. android:textSize="28dip"
  22. android:layout_y="330dip"
  23. android:layout_x="35dip">
  24. </TextView>
  25. <TextView
  26. android:layout_height="wrap_content"
  27. android:layout_width="fill_parent"
  28. android:id="@+id/TextView02"
  29. android:text="圖文並茂,理論清晰,操作性强"
  30. android:textColor="#333"
  31. android:textSize="18dip"
  32. android:layout_y="365dip"
  33. android:layout_x="35dip">
  34. </TextView>
  35. </AbsoluteLayout>

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

接著在 WVGA800 模擬器下檢視顯示效果:

Tip:在絕對定位中,如果子元件沒有設定 layout_x 與 layout_y,它們的預設值為 0,也就是說該元件會像 FrameLayout 一樣出現在左上角。

4、相對定位 RelativeLayout

RelativeLayout 允許子元件指定自己相對於父元件或兄弟元件的位置,是實務上最常用的布局方式之一。它的彈性大上許多,但屬性也多、操作難度較高,屬性之間產生衝突的可能性也更大,因此使用時要多做測試。

以下用 RelativeLayout 重做同樣的範例,先放一張圖片,另外兩個文字則分別相對於上一個元素定位:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout android:id="@+id/RelativeLayout01"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#fff"
  6. xmlns:android="https://schemas.android.com/apk/res/android">
  7. <ImageView android:id="@+id/ImageView01"
  8. android:src="@drawable/android"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginTop="40dip"
  12. >
  13. </ImageView>
  14. <TextView
  15. android:layout_height="wrap_content"
  16. android:layout_width="wrap_content"
  17. android:id="@+id/TextView01"
  18. android:text="Android2.2 學習指南"
  19. android:textColor="#0f0"
  20. android:textSize="28dip"
  21. android:layout_below="@id/ImageView01"
  22. android:layout_centerHorizontal="true"
  23. android:layout_marginTop="10dip">
  24. </TextView>
  25. <TextView
  26. android:layout_height="wrap_content"
  27. android:layout_width="wrap_content"
  28. android:id="@+id/TextView02"
  29. android:text="圖文並茂,理論清晰,操作性强"
  30. android:textSize="18dip"
  31. android:layout_below="@id/TextView01"
  32. android:layout_centerHorizontal="true"
  33. android:layout_marginTop="5dip">
  34. </TextView>
  35. </RelativeLayout>

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

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

可以發現,相對定位的介面效果基本保持一致,而不像絕對定位那樣縮在左上角。畫面能自動縮放,正是採用 dip 作為單位所帶來的好處。關於 dip 的觀念,可參考我在開發小知識中寫的專文。

以下是 RelativeLayout 幾個重要的屬性:

第一類:屬性值為 true 或 false
android:layout_centerHorizontal  水平居中
android:layout_centerVertical  垂直居中
android:layout_centerInparent  相對於父元素完全居中
android:layout_alignParentBottom  貼緊父元素的下邊緣
android:layout_alignParentLeft  貼緊父元素的左邊緣
android:layout_alignParentRight  貼緊父元素的右邊緣
android:layout_alignParentTop  貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing  若找不到對應的兄弟元素,就以父元素作為參照物

第二類:屬性值必須為 id 的參照名「@id/id-name」
android:layout_below  在某元素的下方
android:layout_above  在某元素的上方
android:layout_toLeftOf  在某元素的左邊
android:layout_toRightOf  在某元素的右邊

android:layout_alignTop  本元素的上邊緣與某元素的上邊緣對齊
android:layout_alignLeft  本元素的左邊緣與某元素的左邊緣對齊
android:layout_alignBottom  本元素的下邊緣與某元素的下邊緣對齊
android:layout_alignRight  本元素的右邊緣與某元素的右邊緣對齊

第三類:屬性值為具體的像素值,如 30dip、40px
android:layout_marginBottom  離某元素底邊緣的距離
android:layout_marginLeft  離某元素左邊緣的距離
android:layout_marginRight  離某元素右邊緣的距離
android:layout_marginTop  離某元素上邊緣的距離

接著我們把範例再做一遍,這次加入更多屬性,大家可以親手實驗:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout android:id="@+id/RelativeLayout01"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#cfff" 色彩的設定為 argb,第一個 c 是透明度
  6. xmlns:android="https://schemas.android.com/apk/res/android">
  7. <ImageView android:id="@+id/ImageView01"
  8. android:src="@drawable/android"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_marginTop="40dip"
  12. android:layout_centerHorizontal="true">
  13. </ImageView>
  14. <TextView
  15. android:layout_height="wrap_content"
  16. android:layout_width="wrap_content"
  17. android:id="@+id/TextView01"
  18. android:text="Android2.2 學習指南"
  19. android:textColor="#0f0"
  20. android:textSize="28dip"
  21. android:layout_below="@id/ImageView01"
  22. android:layout_centerHorizontal="true"
  23. android:layout_marginTop="10dip">
  24. </TextView>
  25. <TextView
  26. android:layout_height="wrap_content"
  27. android:layout_width="wrap_content"
  28. android:id="@+id/TextView02"
  29. android:text="圖文並茂,理論清晰,操作性强"
  30. android:textColor="#333"
  31. android:textSize="18dip"
  32. android:layout_below="@id/TextView01"
  33. android:layout_centerHorizontal="true"
  34. android:layout_marginTop="5dip">
  35. </TextView>
  36. <TextView
  37. android:layout_height="wrap_content"
  38. android:layout_width="wrap_content"
  39. android:id="@+id/TextView03"
  40. android:text="alignTop"
  41. android:textColor="#333"
  42. android:textSize="18dip"
  43. android:layout_alignTop="@id/ImageView01" 和 ImageView01 上邊緣對齊
  44. android:layout_centerHorizontal="true">
  45. </TextView>
  46. <TextView
  47. android:layout_height="wrap_content"
  48. android:layout_width="wrap_content"
  49. android:id="@+id/TextView04"
  50. android:text="alignLeft"
  51. android:textColor="#333"
  52. android:textSize="18dip"
  53. android:layout_alignLeft="@id/ImageView01"
  54. android:layout_centerHorizontal="true">
  55. </TextView>
  56. <TextView
  57. android:layout_height="wrap_content"
  58. android:layout_width="wrap_content"
  59. android:id="@+id/TextView05"
  60. android:text="alignRight"
  61. android:textColor="#333"
  62. android:textSize="18dip"
  63. android:layout_alignRight="@id/ImageView01"
  64. android:layout_centerHorizontal="true">
  65. </TextView>
  66. <TextView
  67. android:layout_height="wrap_content"
  68. android:layout_width="wrap_content"
  69. android:id="@+id/TextView06"
  70. android:text="alignBottom"
  71. android:textColor="#333"
  72. android:textSize="18dip"
  73. android:layout_alignBottom="@id/ImageView01"
  74. android:layout_centerHorizontal="true">
  75. </TextView>

今天就先講到這裡。

轉自:https://android.yaohuiji.com/archives/tag/absolutelayout

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