編輯:關於 Android 程式設計
清單檔(AndroidManifest.xml)裡有幾個節點初學時最容易寫錯,因為它們的名稱看起來樸素,實際職責卻差很多。本文挑出 intent-filter、meta-data 與 App Widget 的 receiver 逐項拆解。
先看這段宣告:
<activity android:name=".LunchList"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
<meta-data android:name="android.app.default_searchable"
android:value=".LunchList" />
</activity>
第一組過濾器宣告它是啟動圖示(MAIN + LAUNCHER),第二組則讓它能接收搜尋意圖。這裡的 <category android:name="android.intent.category.DEFAULT" /> 經常被誤認為可省略,但事實相反:凡是自訂的 Activity 想被隱式 Intent 啟動,就必須包含 DEFAULT 類別。因為系統在呼叫 startActivity() 時會自動為隱式 Intent 附加這個 category,缺了它就永遠匹配不上。
這兩行名字相似,作用卻完全不同:
android.app.searchable 搭配 @xml/searchable 資源,用來定義搜尋框的外觀與行為(提示文字、輸入 hint 等),name 是系統約定好的固定值,resource 指向你自己放在 res/xml/ 的設定檔。
android.app.default_searchable 搭配 .LunchList,指定由哪一個 Activity 負責執行搜尋並展示結果。前面那個 meta-data 只在掛載它的那個 Activity 內有效;加上這一個,等於全域宣告預設的搜尋處理者,任何頁面觸發搜尋都會導到 LunchList。這正是「想在任意 Activity 都能叫出搜尋」的關鍵。
<receiver android:name=".AppWidget"
android:label="@string/app_name"
android:icon="@drawable/icon">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
這裡 label 是 widget 在選單中顯示的名稱,icon 是它的圖示。intent-filter 過濾的是 widget 更新廣播,這同樣屬於隱式 Intent,因此 DEFAULT 類別照樣不能省。meta-data 中的 name 為固定值,resource 則指向我們自訂的參數設定檔。
<appwidget-provider xmlns:android="https://schemas.android.com/apk/res/android"
android:minWidth="300dip"
android:minHeight="79dip"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget"
/>
前三行分別是最小寬、最小高與更新間隔(單位為毫秒),第四行指向 widget 的實際佈局。關於 updatePeriodMillis 有一個重要現實:系統對 widget 更新頻率有下限限制,即使寫入更小的值也不會真正生效,短間隔更新應該改用 AlarmManager 或 WorkManager 自行排程。
寫法與 layout 目錄下的一般佈局相同:
<?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:background="@drawable/widget_frame"
>
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/next"
android:textSize="10pt"
android:textColor="#FFFFFFFF"
/>
<ImageButton android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="@drawable/ff"
/>
</RelativeLayout>
這裡有個實務限制值得記住:App Widget 的遠程佈局並非支援全部控制項,只接受 FrameLayout、LinearLayout、RelativeLayout、GridLayout 等少數容器,以及 TextView、ImageView、Button 等基本控制項。自訂 View 放進去不會顯示。另外,widget 內的點擊事件無法用一般的 setOnClickListener(),必須透過 PendingIntent 與 RemoteViews 綁定。
Android 在圖片指定位置動態添加標記點
最近專案裡新增了一個功能,情境是這樣的:假設有一張家居照片,裡面擺放著各式各樣的家具用品——桌子、毛巾、花瓶等等,需要在對應的商品位置加上標記,方便使用者一眼看到商品,點
Android Spinner 用法解析
Spinner 基本概念 Spinner 是下拉式清單控制項,常用來讓使用者從一組選項中做選擇。 版面配置 <Spinner android:id=@+id/Spi
手機QQ怎麼上傳照片到空間 完整圖文步驟
想用手機QQ把照片上傳到空間,具體該怎麼做?下面小編就把步驟走一遍,一起來試試。 1. 先打開手機QQ並登入帳號,點擊下方的「動態」。 2. 進入動態頁後,點擊上方的「好
開發隨筆:會議室推薦邏輯與介面優化
前言這篇開發隨筆整理一個會議室預訂專案在優化過程中的幾個重點,包括介面間距微調、雙 Cursor 查詢、推薦邏輯簡化,以及 Android Studio 外掛安裝。雖然都