編輯:關於 Android 程式設計
前面的文章已經聊過极光推送與登錄 demo,但對於 XML 布局如何擺放控件,筆者自認還不到得心應手的程度。趁這個機會把 Android 中最常用的幾種布局系統梳理一遍。
Android 約定俗成的五大布局如下圖所示。

以下逐一展開說明。
線性布局是日常開發中出現頻率最高的一種,只有兩個方向:垂直方向下每行只放一個元素,多個元素依次向下堆疊;水平方向下只有一行,元素依次向右排開。關鍵屬性是 android:orientation 與 android:layout_weight,後者用來按比例分配剩餘空間。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView android:text="blue" android:background="#52C8FA"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView android:text="yellow" android:background="#FFFF00"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView android:text="pink" android:background="#F60C88"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView android:text="purple" android:background="#722694"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="green" android:background="#39E18A"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:text="pink" android:background="#F60C88"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:text="yellow" android:background="#FFFF00"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:text="blue" android:background="#52C8FA"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
效果如下圖,上下兩個容器各佔一半,內部再分別橫向與縱向排列。

幀布局從屏幕左上角 (0,0) 開始佈局,所有子組件層疊排列:先加入的沉在底層,最後加入的顯示在最上方,上層會遮蓋下層。它適合做圖層覆蓋、浮層提示這類場景。
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="300dp" android:layout_height="300dp"
android:background="#52C8FA" />
<TextView android:layout_width="260dp" android:layout_height="260dp"
android:background="#FFFF00" />
<TextView android:layout_width="220dp" android:layout_height="220dp"
android:background="#F60C88" />
</FrameLayout>
三個不同尺寸的色塊同心疊放,運行效果如下。

TableLayout 是一個以表格形式排列子元素的 ViewGroup,用行與列確定每個 View 的位置。每一行由 TableRow 表示,行內再定義具體單元格;需要跨列時使用 android:layout_span。這五種布局可以互相嵌套,實戰中往往是組合使用。
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<Button android:text="等" android:background="#52C8FA" />
<Button android:text="一" android:background="#FFFF00" />
<Button android:text="個" android:background="#F60C88" />
</TableRow>
<TableRow>
<Button android:text="故" android:background="#722694" />
<Button android:text="事!" android:background="#39E18A"
android:layout_span="2" />
</TableRow>
</TableLayout>
第二行的「事!」通過 layout_span 佔據兩列,效果如下圖。

相對布局以「參照物」的方式定位:某個組件在另一個組件的左邊、右邊、上方或下方,或者相對於父容器對齊。常用屬性包括 layout_below、layout_toLeftOf、layout_alignParentRight 等。需要注意的是,被參照的控件必須先有 id。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px">
<TextView
android:id="@+id/tev1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="請輸入口令,會有驚喜哦" />
<EditText
android:id="@+id/tx1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tev1" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tx1"
android:layout_alignParentRight="true"
android:text="確定"
android:background="#FFFF00" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tx1"
android:layout_toLeftOf="@id/btn1"
android:layout_marginRight="30dp"
android:text="取消"
android:background="#FFFF00" />
</RelativeLayout>
輸入框位於提示文字下方,兩個按鈕並排靠右,效果如下。

絕對布局通過指定精確的 X、Y 坐標來定位子組件。它在 Android 2.0 的 API 文檔中就被標註為 deprecated,官方建議改用 FrameLayout 或 RelativeLayout 替代。原因也很直觀:寫死坐標在不同屏幕密度與分辨率下幾乎必然錯位,因此這裡不再展開。
五種布局各有適用場景:追求簡單排布用線性,需要圖層覆蓋用幀布局,表格化數據用 TableLayout,界面元素關係明確時相對布局最靈活。熟練之後更重要的是判斷何時該嵌套、嵌套多深——層級越淺,measure 與 layout 的開銷越小,這是性能優化裡常被忽略的一環。
Android 仿百度福袋紅包:九宮格解鎖與手勢連線效果
百度福袋紅包以九宮格與手勢連線形成趣味解鎖效果。本文將拆解其概念,說明如何以自訂 ViewGroup 排列圖片,再用透明繪製層畫出連線與圓圈,最後加上縮放動畫。 自訂
我的 Android 進階之旅——查看應用簽名信息
一、查看自己的證書簽名信息 如上一篇《我的 Android 進階之旅——製作和查看自訂的 Debug 版本簽名證書》所述,我們可以透過 keytool 命令查看生成的 k
Android Studio必備設置與高效操作技巧一覽
熟練掌握開發工具,往往能事半功倍,為我們省下大量重複勞動。今天就把Android Studio裡那些值得一開的設置與常用操作整理出來,幫助你把IDE調校得更順手。 一、
Handler 消息傳遞機制原始碼賞析
Android 的訊息處理有四大核心類:Handler、Looper、Message、MessageQueue,全部位於 android.os 套件。 UI 線程與其他線