- Android 基礎
- Android - 首頁
- Android - 概覽
- Android - 環境搭建
- Android - 架構
- Android - 應用元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動 (Activity)
- Android - 服務 (Service)
- Android - 廣播接收器 (Broadcast Receiver)
- Android - 內容提供器 (Content Provider)
- Android - 碎片 (Fragment)
- Android - 意圖/過濾器 (Intents/Filters)
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送郵件
- Android - 傳送簡訊
- Android - 電話呼叫
- 釋出 Android 應用
- Android 實用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕獲
- Android - 音訊管理器
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google 地圖
- Android - 圖片效果
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - LinkedIn 整合
- Android - 載入動畫
- Android - 本地化
- Android - 登入介面
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - Shared Preferences
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 實用資源
- Android - 問題解答
- Android - 實用資源
- Android - 討論
Android - UI 設計
在本章中,我們將瞭解 Android 螢幕的不同 UI 元件。本章還涵蓋了建立更出色 UI 設計的技巧,並解釋瞭如何設計 UI。
UI 螢幕元件
Android 應用的典型使用者介面由操作欄和應用內容區域組成。
- 主操作欄
- 檢視控制
- 內容區域
- 拆分操作欄
這些元件也已在下圖中顯示 -
瞭解螢幕元件
Android 應用的基本單元是活動 (Activity)。UI 在 XML 檔案中定義。在編譯期間,XML 中的每個元素都會編譯成等效的 Android GUI 類,其屬性由方法表示。
檢視 (View) 和檢視組 (ViewGroup)
一個活動 (Activity) 由檢視 (View) 組成。檢視 (View) 只是出現在螢幕上的一個小部件。它可以是按鈕等。一個或多個檢視可以組合成一個 ViewGroup。ViewGroup 的示例包括佈局。
佈局型別
有許多型別的佈局。其中一些列在下面 -
- 線性佈局 (LinearLayout)
- 絕對佈局 (AbsoluteLayout)
- 表格佈局 (TableLayout)
- 框架佈局 (FrameLayout)
- 相對佈局 (RelativeLayout)
線性佈局 (LinearLayout)
線性佈局進一步細分為水平和垂直佈局。這意味著它可以在一列或一行中排列檢視。以下是包含文字檢視的線性佈局(垂直)的程式碼。
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello” />
</LinearLayout>
絕對佈局 (AbsoluteLayout)
AbsoluteLayout 允許您指定其子元素的確切位置。它可以像這樣宣告。
<AbsoluteLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android” >
<Button
android:layout_width=”188dp”
android:layout_height=”wrap_content”
android:text=”Button”
android:layout_x=”126px”
android:layout_y=”361px” />
</AbsoluteLayout>
表格佈局 (TableLayout)
TableLayout 將檢視分組到行和列中。它可以像這樣宣告。
<TableLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_height=”fill_parent”
android:layout_width=”fill_parent” >
<TableRow>
<TextView
android:text=”User Name:”
android:width =”120dp”
/>
<EditText
android:id=”@+id/txtUserName”
android:width=”200dp” />
</TableRow>
</TableLayout>
相對佈局 (RelativeLayout)
RelativeLayout 允許您指定子檢視相對於彼此的位置。它可以像這樣宣告。
<RelativeLayout android:id=”@+id/RLayout” android:layout_width=”fill_parent” android:layout_height=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android” > </RelativeLayout>
框架佈局 (FrameLayout)
FrameLayout 是螢幕上的一個佔位符,您可以使用它來顯示單個檢視。它可以像這樣宣告。
<?xml version=”1.0” encoding=”utf-8”?>
<FrameLayout
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/lblComments”
android:layout_below=”@+id/lblComments”
android:layout_centerHorizontal=”true” >
<ImageView
android:src = “@drawable/droid”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</FrameLayout>
除了這些屬性之外,還有其他屬性在所有檢視和 ViewGroup 中都是通用的。它們列在下面 -
| 序號 | 檢視 & 描述 |
|---|---|
| 1 |
layout_width 指定 View 或 ViewGroup 的寬度 |
| 2 |
layout_height 指定 View 或 ViewGroup 的高度 |
| 3 |
layout_marginTop 指定 View 或 ViewGroup 上方額外的空間 |
| 4 |
layout_marginBottom 指定 View 或 ViewGroup 下方額外的空間 |
| 5 |
layout_marginLeft 指定 View 或 ViewGroup 左側額外的空間 |
| 6 |
layout_marginRight 指定 View 或 ViewGroup 右側額外的空間 |
| 7 |
layout_gravity 指定子檢視如何定位 |
| 8 |
layout_weight 指定應分配給 View 的佈局中額外空間的多少 |
測量單位
在指定 Android UI 上元素的大小時,您應該記住以下測量單位。
| 序號 | 單位 & 描述 |
|---|---|
| 1 |
dp 密度無關畫素。1 dp 在 160 dpi 螢幕上相當於一個畫素。 |
| 2 |
sp 縮放無關畫素。這類似於 dp,建議用於指定字型大小 |
| 3 |
pt 磅。磅定義為 1/72 英寸,基於物理螢幕尺寸。 |
| 4 |
px 畫素。對應於螢幕上的實際畫素 |
螢幕密度
| 序號 | 密度 & DPI |
|---|---|
| 1 |
低密度 (ldpi) 120 dpi |
| 2 |
中密度 (mdpi) 160 dpi |
| 3 |
高密度 (hdpi) 240 dpi |
| 4 |
超高密度 (xhdpi) 320 dpi |
最佳化佈局
以下是一些建立高效佈局的指南。
- 避免不必要的巢狀
- 避免使用太多檢視
- 避免深度巢狀
