編輯:關於 Android 開發
介面是使用者對一個 App 的第一印象,而佈局決定了介面的骨架。這一節把 Android 最基礎的五種佈局逐一拆開來看,順便說清楚該在什麼場合用哪一種。
線性佈局把子元件排成一條線,方向有兩種:水平與垂直。它的好處是簡單直觀,缺點也很明確——不太方便隨意調整單一元件的位置。所以實務上很少單用,多半是線性與相對佈局互相嵌套。

上圖同時示範了水平與垂直兩個方向,可以清楚看到元件如何依序排開。幾個常用屬性:
android:orientation="horizontal" // 水平排列 android:orientation="vertical" // 垂直排列 android:layout_weight="1" // 按比例分配剩餘空間 android:gravity="center_horizontal" // 元件在佈局內的水平對齊方式
layout_weight 是線性佈局最實用的屬性:同一層級的元件若都設為 1,剩餘空間就平均分配。下面用嵌套方式組出「上半水平、下半垂直」的結構:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_weight="2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/jay" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雨松 MOMO"
android:background="#FF0000"
android:textColor="#000000"
android:textSize="18dip" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平方向" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雨松 MOMO"
android:background="#FF0000"
android:textColor="#000000"
android:textSize="18dip" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="垂直方向" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雨松 MOMO" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image" />
</LinearLayout>
</LinearLayout>
相對佈局是 Android 佈局家族裡最強的一員:可用屬性最多,能表達的關係也最豐富。更重要的是,它的座標取值都是相對的——元件相對於父容器或兄弟元件定位,而不是釘死在某個絕對位置。手機螢幕解析度五花八門,相對佈局天生就適合做自適應。

android:layout_alignParentRight="true" // 與父容器右側對齊 android:layout_below="@id/re_edit_0" // 位於 re_edit_0 下方 android:layout_toLeftOf="@id/re_image_0" // 位於 re_image_0 左側 android:layout_alignTop="@id/name" // 與 name 的上緣對齊 android:layout_marginRight="30dip" // 位移像素值
<?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">
<EditText
android:id="@+id/re_edit_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雨松 MOMO"
android:layout_alignParentRight="true" />
<ImageView
android:id="@+id/re_iamge_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/jay"
android:layout_below="@id/re_edit_0"
android:layout_alignParentRight="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="努力學習"
android:textColor="#000000"
android:textSize="18dip"
android:layout_toLeftOf="@id/re_iamge_0" />
<EditText
android:id="@+id/re_edit_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雨松 MOMO"
android:layout_alignParentBottom="true" />
<ImageView
android:id="@+id/re_iamge_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image"
android:layout_above="@id/re_edit_1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="努力工作"
android:textColor="#000000"
android:textSize="18dip"
android:layout_toRightOf="@id/re_iamge_1"
android:layout_above="@id/re_edit_1" />
</RelativeLayout>
使用相對佈局時有個順序上的注意事項:被引用的元件必須先宣告,或者至少要有 id,否則會拿到錯誤的參照。
框架佈局的規則很單純:所有子元件疊在同一個位置上,後畫的蓋住先畫的,最上層永遠是最後一個。它適合需要堆疊的場合,例如相片上面浮一層文字。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/g" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雨松 MOMO"
android:background="#FF0000"
android:textColor="#000000"
android:textSize="18dip" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image"
android:layout_gravity="bottom" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="快樂生活每一天喔"
android:layout_gravity="bottom" />
</FrameLayout>
圖中的 ImageView 先畫,之後的 TextView 與 EditText 就疊在它上面。搭配 android:layout_gravity 可以讓每個子元件各自靠齊不同邊,不至於全擠在左上角。
絕對佈局讓你直接指定每個元件在螢幕上的 X、Y 座標。聽起來很自由,但筆者不建議使用——理由還是那句話:Android 手機解析度五花八門,寫死座標的介面換一台機器就散了。它和框架佈局一樣,後畫的會蓋住先畫的。

android:layout_x="50dip" android:layout_y="30dip"
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/f"
android:layout_x="100dip"
android:layout_y="50dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="當前座標點 x = 100dip y = 50dip"
android:background="#FFFFFF"
android:textColor="#FF0000"
android:textSize="18dip"
android:layout_x="50dip"
android:layout_y="30dip" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/h"
android:layout_x="50dip"
android:layout_y="300dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="當前座標點 x = 50dip y = 300dip"
android:background="#FFFFFF"
android:textColor="#FF0000"
android:textSize="18dip"
android:layout_x="30dip"
android:layout_y="280dip" />
</AbsoluteLayout>
了解它的原理有價值——特別是讀懂別人的老程式碼時——但新專案裡基本用不到。
表格佈局以列為單位組織內容,透過 TableRow 定義每一列,可以控制欄位內容、對齊方式與內距,適合呈現結構化的資料。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/g"
android:layout_gravity="center" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView android:text="姓名" android:gravity="left" />
<TextView android:text="電話" android:gravity="right" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FFFFFF" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView android:text="雨松" android:gravity="left" />
<TextView android:text="15810463139" android:gravity="right" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView android:text="小可愛" android:gravity="left" />
<TextView android:text="15810463139" android:gravity="right" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView android:text="姓名" android:gravity="left" />
<TextView android:text="性別" android:gravity="right" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FFFFFF" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView android:text="雨松 MOMO" android:gravity="left" />
<TextView android:text="男" android:gravity="right" />
</TableRow>
</TableLayout>
那兩段 View 是分隔線的小技巧:高度給 2dip、塞一個背景色,就能在列與列之間畫出一條線。
五種佈局的基本用法講完了,最後還是回到實務選擇:
layout_weight 做比例分割。真正的關鍵其實是嵌套:把這幾種佈局組裝起來,才能做出既好看又能適配各種螢幕的介面。不必執著於單一佈局,依實際情況選用最省事的那一種就好。
源碼下載地址:https://vdisk.weibo.com/s/aa2C2
Android 介面設計基本功:布局屬性、LayoutInflater 與列表控制項
Android 介面設計基本知識 一個好的 App 不能只有好看的介面,還要有良好的效能與穩定性。身為開發人員,得理解介面設計的原則,並寫出可靠的布局程式碼。以下整理常用
Android 實現啟動頁倒計時的兩種方案
需求場景 打開愛奇藝、京東這類應用時,啟動頁右上角往往有個倒計時控件,倒數結束後自動跳轉主介面。這個效果看起來簡單,實現方式卻有講究。下面給出兩種做法。 方法一:Time
把語音搜尋接進 Google Now:讓「OK Google」直接呼叫你的 App
把語音搜尋接進 Google Now:讓「OK Google」直接呼叫你的 App 原文標題:Use Voice Search to integrate with Goo
關於 Android 滑動衝突的解決方法(二):同方向嵌套滾動
同方向滑動衝突的破解思路 上一篇筆記談的是不同方向滑動造成的衝突,那種情況好理解也好解決。這次我們把視角轉向同一方向的滑動衝突,並以垂直方向為背景——這也是日常開發中最常