編輯:Android 開發實例
本講將介紹兩種常用的 Layout:AbsoluteLayout(絕對定位)與 RelativeLayout(相對定位),並透過同一個範例對照兩者的差異。點此下載:Android 學習指南第六講源碼。
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 一樣出現在左上角。
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:textSize="18dip"
- android:layout_below="@id/TextView01"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="5dip">
- </TextView>
- </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 離某元素上邊緣的距離接著我們把範例再做一遍,這次加入更多屬性,大家可以親手實驗:
- <?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>
今天就先講到這裡。
轉自:https://android.yaohuiji.com/archives/tag/absolutelayout
Android MediaPlayer:打造自己的音樂播放器
要在 Android 上播放音訊或影片,最直接的方式就是使用 MediaPlayer 類別。它是系統內建媒體播放服務的 Java 介面,音訊、影片、串流都吃得住,同時 a
Android 登入功能範例與錯誤處理
登入功能通常包含帳號輸入、密碼輸入、登入按鈕與錯誤處理。本文示範如何建立一個具備三次嘗試限制的 Android 登入畫面,並在失敗次數用盡後停用登入按鈕。 版面配置 在
Android 頂部導航菜單左右滑動的兩種實作方案
本文示範在 Android 中實作頂部導航菜單的左右滑動效果,提供兩種方案:以 ViewPager 分頁切換,以及以 HorizontalScrollView 做連續滑動
Android 進度提示元件:Spinner 與 ProgressBar 的實作方式
在 Android 上,若要向使用者傳達「工作進行中」,最直接的方式就是顯示進度條。ProgressBar 有兩種外觀:可標示百分比的長條式進度條,以及旋轉式的 spin