編輯:關於 Android 程式設計
前言
從 Android 4.4 開始,系統提供了沉浸式全屏能力:狀態欄與虛擬按鍵可以動態隱藏,應用得以佔用完整的屏幕空間,按 Google 的說法,這能給用戶一種「身臨其境」的觀感。到了 Android 5.0,又拋出了 ColorPalette 的概念,允許開發者自定義系統區域的顏色,讓 App 的視覺風格與系統融為一體。
本文就來梳理一下,如何在 4.4 及以上版本實現全屏沉浸式透明狀態欄。先看預期效果:

動手之前,先把兩個容易混淆的概念分清楚:屏幕頂部是狀態欄 StatusBar,底部是導航欄 NavigationBar。
狀態欄如下:

導航欄如下:

具體怎麼做
1. 先實現全屏
第一種:直接繼承特定主題
API 19 以上可以使用帶 TranslucentDecor 的主題,它們本身就自帶半透明效果。新增的 Theme.Holo.NoActionBar.TranslucentDecor 和 Theme.Holo.Light.NoActionBar.TranslucentDecor 只在 4.4 以上存在,所以要在 values-v19 文件夾下單獨建 styles 文件:
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor"> <!-- Customize your theme here. --> </style>
第二種:在 Activity 裡用代碼設置
Android 4.4 以上可以這樣寫:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }Android 5.0 以上則有另一套寫法:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); }全屏效果如下:

可以看到狀態欄的位置空出來了,同時和 App 的布局疊在了一起。這個問題得接著處理。
2. 解決狀態欄佔位問題
三種方式,按場景挑一種即可。
主題裡加:
<item name="android:fitsSystemWindows">true</item>
布局根節點加:
android:fitsSystemWindows="true"
代碼設置:
rootview.setFitsSystemWindows(true);
fitsSystemWindows 只在 SDK >= 19 的系統上生效,它可以設置在任何 View 上;一旦設置,該 View 自身的 padding 屬性就會失效。更重要的是,它必須配合透明狀態欄或透明導航欄才有意義——此時系統會自動給這個 View 加上一個等於狀態欄高度的 paddingTop,以及等於導航欄高度的 paddingBottom。
效果如下:

3. 給狀態欄和導航欄上色
4.4 以上的做法是改 contentView 的背景色,或者往 contentView 上動態加一個 View:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); //設置contentview為fitsSystemWindows ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } //給statusbar著色 View view = new View(this); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this))); view.setBackgroundColor(color); contentView.addView(view); }動態獲取狀態欄高度:
/** * 獲取狀態欄高度 * * @param context context * @return 狀態欄高度 */ private static int getStatusBarHeight(Context context) { // 獲得狀態欄高度 int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); }動態獲取導航欄高度:
/** * 獲取導航欄高度 * * @param context context * @return 導航欄高度 */ public static int getNavigationBarHeight(Context context) { int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); }而在 5.0 以上,系統提供了更直接的 API:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //設置狀態欄顏色 window.setStatusBarColor(color); //設置導航欄顏色 window.setNavigationBarColor(color); ViewGroup contentView = ((ViewGroup) findViewById(android.R.id.content)); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } // contentView.setPadding(0, getStatusBarHeight(this), 0, 0); }至此整體效果就出來了。

4. 完整的 Java 實現
private void initWindows() { Window window = getWindow(); int color = getResources().getColor(android.R.color.holo_blue_light); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //設置狀態欄顏色 window.setStatusBarColor(color); //設置導航欄顏色 window.setNavigationBarColor(color); ViewGroup contentView = ((ViewGroup) findViewById(android.R.id.content)); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); //設置contentview為fitsSystemWindows ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content); View childAt = contentView.getChildAt(0); if (childAt != null) { childAt.setFitsSystemWindows(true); } //給statusbar著色 View view = new View(this); view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this))); view.setBackgroundColor(color); contentView.addView(view); } }總結
本文為了讓效果更直觀,故意把狀態欄和標題欄設置成了不同顏色。實際項目裡通常會讓它們保持一致,視覺上更統一,用戶也更容易真正沉浸到內容裡。以上就是關於 Android 全屏沉浸式透明狀態欄的實現梳理,如有疑問歡迎留言,我會及時回覆。
React Native Alert 用法與實例
Alert 是 React Native 中常見的警告框元件,通常用於退出登入、清除快取、提示修改密碼等需要使用者確認的操作。 基本用法 React Native 的
Android 上 line-height 垂直居中失效的成因與解法
講到 line-height,做過網頁的朋友應該都不陌生——拿來做垂直居中屢試不爽,幾乎沒出過差錯。但最近一個專案,測試提了一個 Bug,看圖就明白: 這個問題只在 A
Android Fragment 完全解析:建立、管理、通訊與生命週期
Fragment 概述 Fragment 代表 Activity 中的一部分 UI 或行為。一個 Activity 可以組合多個 Fragment,Fragment 也能
深入 Android Service 的 bindService 使用機制
前面我們已經了解了以 startService 方式啟動服務的用法,現在來看 Service 的另一種啟動方式——bindService。 bindService 的使用