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

Android 布局實戰:絕對布局 AbsoluteLayout 與相對布局 RelativeLayout 深度對比

編輯:關於 Android 程式設計

本文以實例方式,帶大家完整分析 Android 中兩種經典布局:絕對布局 AbsoluteLayout 與相對布局 RelativeLayout。兩者定位思路截然不同,理解後能幫助你在開發時做出更好的取捨。以下內容供大家參考。

一、絕對布局 AbsoluteLayout

絕對定位的 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 模擬器下觀察:

小提醒:在絕對定位中,如果子元素沒有設定 layout_x 與 layout_y,它們的預設值都是 0,此時元素會像 FrameLayout 一樣被擺在左上角。

二、相對布局 RelativeLayout

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
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 離某元素上邊緣的距離

接著我們把上面的例子再做一次,這回多加幾個屬性,方便大家實際動手試試:

 <?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> 

關於絕對布局 AbsoluteLayout 與相對布局 RelativeLayout 的內容就介紹到這裡,希望本文對大家的 Android 介面開發有所啟發。

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