編輯:關於 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
第二類:屬性值必須為 id 的引用名「@id/id-name」
第三類:屬性值為具體像素值,如 30dip、40px
我們把上面的例子再做一遍,這次加入更多屬性,大家可以親手試驗:
<?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 的介紹,後續將繼續整理相關資料。
Android 遊戲開發入門:一個小球彈跳的完整實例
能在 Android 上寫出遊戲,是很多開發者入門時的動力所在。本文從零做一個極簡示例:一顆小球在屏幕內四壁反彈。麻雀雖小,SurfaceView、雙緩衝、線程與幀率控制
Android 自訂 View:模擬水波效果的實作
歡迎 Fork,也歡迎 Star。 先看效果 再看關鍵程式碼 整個水波效果的核心,其實就是那條擺動的正弦曲線,對應的數學式為 y = A·sin(ωx + d) + o
Android 事件分發源碼解析(上):View 層的完整執行鏈路
一、前言 讀完《Android 開發藝術探索》第三章,我對事件分發機制有了整體輪廓。網路上討論這個主題的文章很多,自己也翻了一些,但始終覺得不如自己跟一遍源碼來得踏實。所
Android 實戰:實現九宮格圖片連續滑動效果
本文將探討如何在 Android 中實現 GridView(九宮格)內每個宮格圖片的左右滑動瀏覽,並在當前宮格圖片展示完畢後自動切換到下一個宮格的效果。這一交互在新聞類客