編輯:關於 Android 程式設計
Android 提供了一個專用的 XML 命名空間,讓開發工具得以在資源檔裡記錄額外資訊,而這些資訊在打包時會被剝除,不影響執行期效能與 APK 體積。
命名空間 URI 是 https://schemas.android.com/tools,慣例綁定到 tools: 前綴:
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
</FrameLayout>
以下逐一說明目前常用的 tools 屬性(注意:這批屬性會隨版本演進而增減)。
可放在任何元素上,值為逗號分隔的 lint 問題 ID 清單,表示此元素及其所有子元素遞迴忽略這些檢查。
<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>
用途:Lint。
效果等同 Java 的 @TargetApi 註解,用來標示此元素需要某個 API level(整數或代碼名稱)以上才適用。
<GridLayout tools:targetApi="ICE_CREAM_SANDWICH" >
用途:Lint。
設定在資源檔的根元素上,對應語言或地區,讓工具知道檔案內字串假設是哪種語言。例如:
<resources xmlns:tools="https://schemas.android.com/tools"
tools:locale="es">
如此一來,工具便知道預設 values 資料夾裡的字串是西班牙文而非英文。
用途:Lint、Studio(用於停用非英文資源檔的拼字檢查)。
通常設在版面 XML 的根元素,標明這個版面關聯到哪個 Activity。因為主題定義在 Manifest 且與 Activity 綁定,Layout Editor 便得以推斷預設主題。可仿照 Manifest 用點號開頭簡寫:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
tools:context=".MainActivity" ... >
用途:Studio 與 Eclipse 的版面編輯器、Lint。
設在 <fragment> 標籤上,指定設計檢視時要渲染的版面(執行期實際顯示哪個版面,由 Fragment 邏輯決定)。
<fragment
android:name="com.example.master.ItemListFragment"
tools:layout="@android:layout/list_content" />
用途:Studio 與 Eclipse 的版面編輯器。
用於設計期的 ListView(以及 GridView、ExpandableListView 等 AdapterView 子類),指定列表項、頁首與頁尾的版面。工具會填入假資料,讓你看得到列表的大致樣貌。
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@android:layout/simple_list_item_2" />
用途:Studio 與 Eclipse 的版面編輯器。
設在一個會被 <include> 的版面之根元素上,指向包含它的其中一個版面。這樣被包含的版面在設計時會連同外層版面一起渲染,讓你能「在情境中」編輯。需要 Studio 0.5.8 以上版本。
<TextView xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:showIn="@layout/activity_main" />
用途:Studio 的版面編輯器。
設在版面根元素,用來指定 Action Bar 上要顯示的選單。Studio 預設會去關聯 Activity(透過 tools:context)的 onCreateOptionsMenu() 推測,這個屬性讓你能手動覆寫。值為逗號分隔的 ID 清單,不加 @id/ 之類前綴,也可直接寫選單 XML 的檔名(省略 .xml)。需要 Studio 0.8.0 以上版本。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:menu="menu1,menu2" />
用途:Studio 的版面編輯器。
同樣設在根元素,指定 Action Bar 的導覽模式,可選值為 standard、list、tabs。需要 Studio 0.8.0 以上版本。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:actionBarNavMode="tabs" />
用途:Studio 的版面編輯器。
除了上述 tools 專屬屬性,版面中任何既有的 Android 屬性都能加上 tools: 前綴,做為「僅設計期生效」的覆寫值。最典型的用法是 tools:text——讓你在預覽時看到示意文字,卻不影響執行期顯示的內容。這對 ListView 項目、自訂 View 的預覽特別有用。
Android 通知欄 Notification 詳解
Notification 是 Android 中常見的使用者提醒機制,可用於顯示新訊息、下載進度、背景服務狀態或系統提示。開發者可以透過 NotificationMana
SparseArray 取代 HashMap 的實測分析
SparseArray 是 Android 框架獨有的類別,標準 JDK 裡並沒有這個東西。官方說法認為它比 HashMap 更省記憶體,某些情境下效能也更好,原因主要有
Android 中的消息傳遞:詳解廣播機制
廣播機制簡介 Android 中的廣播機制非常靈活,每個應用程序都可以對自己感興趣的廣播進行註冊,並且只會收到自己關心的內容。這些廣播可能來自系統,也可能來自其他應用程序
Android 菜鳥成長筆記(8):Intent 與 Intent Filter 入門(上)
Intent 代表的是 Android 應用的啟動「意圖」。系統會依據 Intent 上的各項屬性來決定要啟動哪一個元件,而具體啟動誰,取決於你怎麼描述這個意圖。 一、