編輯:關於 Android 程式設計
在開發一個與通知列相關的功能時遇到一個限制:專案是插件化的架構,無法引入系統的布局資源檔,也就等於無法用標準的 RemoteViews 自訂版面,連通知列的 icon 都換不了。
網路上流傳一種不靠版面檔換 icon 的做法,但由於 Android 開放性帶來的碎片化——部分手機廠商會直接改動通知列的原始碼——這類偏門技巧的相容性相當有限。有興趣可以參考:https://blog.csdn.net/z1074971432/article/details/10446715
為了覆蓋大多數機型,這裡衍生出一種「曲線救國」的折衷方案:直接拿系統正在使用的那個通知版面,把它裡面的 ImageView 找出來替換掉。
關鍵在於 Notification.contentView 這個欄位。它本來是我們用來塞自訂 RemoteViews 的地方,但如果反過來,先用一個空的 Notification 讓系統幫我們 apply 出預設版面,就能拿到一個真實的 View 樹,再逐一走訪其中的子檢視。
public void show(String title, CharSequence content, Bitmap bitmap, PendingIntent intent) {
// 在 2.3 的機子上,若 id 相同的 notification 一個帶 ContentIntent 一個不帶,會拋異常
_notification.setLatestEventInfo(_context, "", "", null);
_notification.flags = _flag;
RemoteViews hide = _notification.contentView;
initView(_notification, hide, title, content, intent, bitmap);
_manager.notify(_id, _notification);
}
public void cancel() {
_manager.cancel(_id);
}
真正的重頭戲在 initView():
@SuppressWarnings("deprecation")
void initView(final Notification bar, final RemoteViews views, String title,
CharSequence text, PendingIntent intent, final Bitmap bitmap) {
bar.contentView = null;
bar.setLatestEventInfo(_context, title, text, intent);
// 以一個臨時 notification 讓系統 apply 出預設版面,取得真實的 View 樹
Notification notification = new Notification();
notification.setLatestEventInfo(_context, "", "", null);
View view = notification.contentView.apply(_context, null);
ViewGroup group = (ViewGroup) view;
findView(group, new ViewVisitor() {
@Override
public void onFindView(View item) {
if (item instanceof ImageView) {
// 將原有 icon 隱藏:某些小米機型上系統 icon 會覆蓋我們的設定
bar.contentView.setInt(item.getId(), "setAlpha", 0);
views.setViewPadding(item.getId(), item.getPaddingLeft(),
item.getPaddingTop(), item.getPaddingRight(),
item.getPaddingBottom());
if (bitmap != null) {
views.setImageViewBitmap(item.getId(), bitmap);
} else {
views.setImageViewResource(item.getId(), _context.getApplicationInfo().icon);
}
} else if (item instanceof TextView) {
// 隱藏最上層所有 TextView,避免與底層文字重疊
views.setViewVisibility(item.getId(), View.GONE);
}
}
});
// 上層版面背景設為全透明
views.setInt(view.getId(), "setBackgroundColor", Color.argb(0, 0, 0, 0));
views.setViewPadding(view.getId(), 0, view.getPaddingTop(),
view.getPaddingRight(), view.getPaddingBottom());
bar.contentView.addView(view.getId(), views);
}
走訪子檢視的遞迴工具,用一個簡短的 Visitor 介面把回呼邏輯外放,避免把判斷寫死在遞迴裡:
public interface ViewVisitor {
void onFindView(View view);
}
void findView(ViewGroup group, ViewVisitor visitor) {
for (int i = 0; i < group.getChildCount(); i++) {
View view = group.getChildAt(i);
if (visitor != null) {
visitor.onFindView(view);
}
if (view instanceof ViewGroup) {
findView((ViewGroup) view, visitor);
}
}
}
由於無法對自訂版面做精確定位,這條路的 icon 尺寸與版面間距,難免會和原廠系統樣式有些許偏差。
更根本的問題是:整個做法建立在「系統通知版面的 id 結構固定」這個假設上,而這恰恰是各版本、各品牌差異最大的地方。因此它適合的是「插件架構下必須換 icon、且能接受視覺上不完美」的過渡情境。若專案可以正常打包資源檔,仍應優先採用 NotificationCompat.Builder 搭配 setSmallIcon(),並遵循 Android 8.0 之後的通知渠道(Notification Channel)規範——那才是長遠可維護的做法。
Android 用戶端登入與自動登入實作詳解(下)
上一篇我們已經談過 Android 用戶端如何與伺服器互動、完成註冊流程(可參考〈Android 用戶端實作註冊/登入詳解(一)〉)。本文接著往下談登入與自動登入該怎麼做
Universal-Image-Loader 完全解析:從使用到源碼的圖片加載框架全攻略
做 Android 的過程中,幾乎每個人都要面對異步加載圖片這件事,尤其是列表裡一次性載入幾十上百張圖的時候。圖片加載從來不是「new 一個 Bitmap 塞進 Imag
Android ProgressBar 反向進度條:讓進度從右向左走
在項目開發中,我們時常會遇到這樣的需求:用條形圖展示某個比例,並且要求右對齊。此時與其手動繪製,不如直接複用系統的 ProgressBar,既省事又美觀。 那麼,如何讓
Android WebView 使用要點:JavaScript、返回鍵與頁面狀態
WebView 可以視為應用內的迷你瀏覽器,使用 WebKit 核心,能載入 HTML、CSS 與 JavaScript 內容。使用時有幾個關鍵設定:首先必須在 Andr