想讓 App 的主色調延伸至系統通知欄(Status Bar),打造沉浸式視覺體驗?Android 4.4 (API 19) 起提供透明狀態欄 API,但原生實作繁瑣。開源庫 SystemBarTint 以極簡 API 解決此痛點,是開發者首選方案。
repositories { maven { url 'https://jitpack.io' } }
dependencies { implementation 'com.github.jgilfelt:SystemBarTint:v1.0.3' }
onCreate 中啟用:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.colorPrimary); // 你的主色調
}
| Android 版本 | API Level | 支援狀態 |
|---|---|---|
| 4.4 KitKat | 19 | 半透明狀態欄,可著色(需透明背景配合) |
| 5.0 Lollipop | 21 | 原生 Window.setStatusBarColor,庫自動轉呼叫 |
| 6.0+ | 23+ | 完整支援,含深色圖示模式 |
fitsSystemWindows="true" 於根佈局,避免內容被狀態欄遮蓋tintManager.setStatusBarTintColor(newColor) 即時更新tintManager.setNavigationBarTintEnabled(true)注意:API 19 僅支援「透明狀態欄+著色」,真正的 Material Design 沉浸式體驗建議以 API 21+ 為基準開發,低版本優雅降級即可。