編輯:關於 Android 程式設計
桌面小部件(App Widget)讓使用者在不出現應用的情況下就能看到關鍵信息。它的開發步驟比想像中繁瑣一些,但骨架一旦搭好,後續都是重複勞動。以下以一個需要登錄後才能顯示信息的示例說明全流程。
建立一個類並繼承 AppWidgetProvider。它提供了 onDelete、onEnable、onDisabled 等多個回呼,但實務中絕大多數情況只需要覆寫 onUpdate(Context, AppWidgetManager, int[])。在這個方法裡,通常會啟動後台服務(Thread 或 Service)去拉取服務器數據,再回寫到 Widget 上。
<receiver android:name="JiwaiWidget"
android:label="@string/app_name"
android:icon="@drawable/jiwai">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/info" />
</receiver>
逐行說明:android:name 指向你的 AppWidgetProvider 子類;android:label 是長按 Widget 時菜單裡顯示的名稱,取自 string.xml;android:icon 是同一菜單中的圖標;intent-filter 中的 APPWIDGET_UPDATE 是固定寫法,用於接收更新廣播;最後的 meta-data 指向描述文件,其中定義了尺寸、更新間隔等屬性。
在 res/xml 下建立 info.xml:
<appwidget-provider xmlns:android="https://schemas.android.com/apk/res/android"
android:minWidth="200dp"
android:minHeight="90dp"
android:updatePeriodMillis="43200000"
android:initialLayout="@layout/appwidget"
android:configure="com.lawrenst.jiwai.JiwaiConfigure">
</appwidget-provider>
updatePeriodMillis 是系統自動更新的間隔(毫秒),initialLayout 是界面佈局,configure 則是可選的:若 Widget 需要先讓使用者輸入帳號密碼,就把它指向一個 Activity,該 Activity 的返回值會被當作 Widget 的初始配置。
在 layout 目錄下建立 appwidget.xml,包含一個顯示用戶名的標題欄和兩行信息:
<?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="wrap_content"
android:orientation="vertical"
android:id="@+id/widget"
android:background="@drawable/title_a">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:background="@drawable/title">
<TextView
android:id="@+id/username_display"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:textSize="15px"
android:gravity="left|center_vertical"
android:paddingLeft="6px" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:textColor="#ffffff"
android:textSize="12px"
android:gravity="center_vertical|left"
android:paddingLeft="6px"
android:layout_height="30px">
</TextView>
<TextView
android:id="@+id/text2"
android:textColor="#ffffff"
android:layout_height="30px"
android:gravity="center_vertical|left"
android:textSize="12px"
android:paddingLeft="6px"
android:layout_width="fill_parent">
</TextView>
</LinearLayout>
</LinearLayout>
由於需要先取得帳號密碼,在 layout 下新增 login.xml,包含兩個 EditText 與一個 Button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="#ff8c00"
android:capitalize="characters" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user"
android:textColor="#ff8cff"
android:capitalize="characters" />
<EditText
android:id="@+id/username"
android:layout_width="200px"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/code"
android:textColor="#ff8cff"
android:capitalize="characters" />
<EditText
android:id="@+id/password"
android:layout_width="200px"
android:layout_height="wrap_content"
android:password="true" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</LinearLayout>
Widget 的界面並不是普通的 View 體系,而是由 RemoteViews 描述、跑在 Launcher 進程裡的一份「遠程佈局」。這帶來三個硬限制:只支持有限的幾種控件(FrameLayout、LinearLayout、RelativeLayout、TextView、ImageView、ProgressBar 等),不支持自定義 View;不能像往常那樣直接 findViewById 後 setText,必須透過 remoteViews.setTextViewText() 這類方法操作,再呼叫 AppWidgetManager.updateAppWidget() 提交;點擊事件只能用 setPendingIntent 綁定。
另外提醒:updatePeriodMillis 實際上並不可靠,系統為了省電會把多個 Widget 的更新合併到同一批次觸發,最小間隔也常被拉長。需要更精準的刷新節奏時,建議改用 AlarmManager 或 WorkManager 主動推送更新。
Android TabHost 標籤頁的四種實作方式
Tab 標籤是 Android 早期常見的 UI 元件,核心類別是 TabHost。本文用四個由淺入深的例子把它的用法完整走一遍,最後再談談為什麼現在已經不建議使用它。
Android 實戰技巧之四十五:讓既有 C 程式碼在 Android 平台上重獲新生
任務背景 手上有一套以 C 語言打造、代號為「Shooter」的核心演算法庫,它能切實解決我們當前遭遇的幾個難題。遺憾的是,這套庫過去一直運行在其他平台上。因此,這次的核
Android 視圖狀態及重繪流程分析:深入了解 View
視圖狀態是互動效果的核心 Android View 具有多種狀態,例如 enabled、focused、selected、pressed 與 window_focused
Android 屬性動畫入門:從 ObjectAnimator 到自訂 TypeEvaluator
網路上高手的開場白往往先談心境、聊生活,我這裡就省掉鋪墊,直接進主題——因為這東西本來就不難。 動畫的兩套體系 Android 的動畫效果繞不開四件事:位移、縮放、旋轉、