編輯:Android 開發教學
LinearLayout 與 TableLayout 已能滿足大部分介面需求,但有時候會不夠靈活。這時可以考慮 RelativeLayout。
RelativeLayout 是一種相對佈局容器,其內部的子元素可以用「彼此之間的相對位置」或「與容器的相對位置」來定位,概念上有點像網頁設計中的 CSS。
指定控件位置時,你要說明它與其他控件的相對關係——在另一個控件的左邊、右邊、上方、下方,或與某個邊緣對齊等等。一個控件可以同時指定與多個其他控件的相對位置,這讓設計層次豐富的介面變得相當方便。
// 相對於給定 ID 的控件 android:layout_above 把該控件的底部置於給定 ID 控件之上 android:layout_below 把該控件的底部置於給定 ID 控件之下 android:layout_toLeftOf 該控件右邊緣與給定 ID 左邊緣對齊 android:layout_toRightOf 該控件左邊緣與給定 ID 右邊緣對齊 android:layout_alignBaseline 與給定 ID 的 baseline 對齊 android:layout_alignTop 頂部邊緣與給定 ID 頂部對齊 android:layout_alignBottom 底部邊緣與給定 ID 底部對齊 android:layout_alignLeft 左邊緣與給定 ID 左邊緣對齊 android:layout_alignRight 右邊緣與給定 ID 右邊緣對齊 // 相對於父容器 android:layout_alignParentTop 頂部與父容器頂部對齊 android:layout_alignParentBottom 底部與父容器底部對齊 android:layout_alignParentLeft 左部與父容器左部對齊 android:layout_alignParentRight 右部與父容器右部對齊 // 居中 android:layout_centerHorizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInParent 置於父容器正中央 // 邊距偏移(px) android:layout_marginTop / Bottom / Left / Right
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10px" >
<TextView
android:id="@+id/textView"
android:text="TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- 沒有做位置設定,預設在容器的左上角 -->
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:background="@android:drawable/editbox_background" />
<!-- editText 位於 textView 下方 -->
<Button
android:id="@+id/buttonSure"
android:text="確定"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px" />
<!-- 在 editText 下方,並相對於父容器靠右對齊。
marginLeft 讓它與右側其他內容間隔 10 像素 -->
<Button
android:id="@+id/buttonCancel"
android:text="取消"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/buttonSure"
android:layout_alignTop="@id/buttonSure" />
<!-- 相對於 buttonSure 定位:右邊緣與 buttonSure 左邊緣對齊,
頂部與 buttonSure 頂部對齊 -->
</RelativeLayout>
第一,被參照的 View 必須先宣告。RelativeLayout 是一次性解析的,若 layout_below="@id/xxx" 指向一個在它之後才定義的 View,系統會找不到參照目標而無效化該約束。範例中 TextView 寫在最前面正是這個原因。
第二,用 @id 而非 @+id 參照既有 View。@+id 是「建立這個 id」,@id 是「參照已存在的 id」。在 layout_below 這類約束屬性中應該用 @id,用 @+id 雖然通常也能運作,但語意上不精確。
以現在的標準來看,RelativeLayout 已被 ConstraintLayout 取代。ConstraintLayout 表達能力更強(支援百分比、比例鏈、屏障、Guideline),而且能保持扁平層次,效能更好。Android Studio 的 Layout Editor 也預設使用它。
不過理解 RelativeLayout 仍有價值:ConstraintLayout 的約束概念與它一脉相承,學過相對定位再看 Constraint,幾乎是無縫接軌。維護舊專案時更是必備知識。
理解Android Build系統:Make、模組與客製化編譯
Android Build系統是Android源碼的重要組成部分,負責編譯系統、SDK與相關文件。它主要由Make檔、Shell腳本與Python腳本組成,其中Make檔
Android 用戶視角:iOS 7 與 Android 4.2 功能全面對比
蘋果 WWDC 落幕後,知名 Android 論壇 Droid-life 發布了一篇 iOS 7 與 Android 4.2 的功能對比。作者以 Android 用戶的立
android的系統目錄說
Android 以 Linux 為基礎,因此目錄結構與常見 Linux 系統相似。理解根目錄下的重要資料夾,有助於除錯、效能分析與系統知識建立。常見目錄mnt:掛載點目錄
Android 開發教學:平台架構與特性概述
Android 是針對行動裝置的軟體平台,包含作業系統、中介軟體與應用。特性支援元件重用、Dalvik 虛擬機、SQLite、多媒體、網路與豐富開發工具。架構應用框架提供