Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 4.4 以上沉浸式狀態欄的兩種實作思路

編輯:關於 Android 程式設計

先搞清楚:沉浸式狀態欄到底是什麼

一句話概括,就是讓狀態欄的底色跟著 App 本身的配色走,使頂部與內容融為一體。以往我們打開一個應用,頂部總有一條黑邊把界面硬生生切開,視覺上非常割裂。而沉浸式狀態欄正是為了消除這條突兀的分界線,讓使用者第一眼看到的是完整的設計稿。也正因如此,它成了近年來提升用戶體驗的常見做法,被越來越多應用採用。

背後的原理

從 Android 4.4(KitKat)開始,系統提供了狀態欄透明的ability,對應的標誌位是 WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS。

一旦加上這個 flag,布局的根容器 DecorView 就會自動向上延伸填滿狀態欄區域。等於說系統預設幫你「騰空」了狀態欄那一條高度,剩下的就交給開發者自由發揮——你可以放一個自定義 View 去填它,也可以讓下面的內容自己頂上去。

另一個關鍵屬性是 android:fitsSystemWindows="true"。它的含義是:該 View 會依照系統窗口(狀態欄、軟鍵盤等)調整自身布局,當值為 true 時,系統會自動為它加上相應的 padding,把系統窗口的高度讓出來。

下面直接看兩種常見做法。

做法一:單獨給狀態欄上色

這一類通常配合開源庫 SystemBarTint 使用,核心只有一個類,直接把源碼拷進項目即可:

/** 狀態欄著色
 * @param context 上下文
 * @param color 目標顏色 */
public static void smartTintManager(Activity context, int color) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window window = context.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        // 建立狀態欄管理實例
        SystemBarTintManager tintManager = new SystemBarTintManager(context);
        // 啟用狀態欄著色
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintColor(color);
    }
}

使用上有兩個容易踩雷的地方:頁面的根布局要加上 android:fitsSystemWindows="true";同時根布局不要設定整體的大背景色,否則會把狀態欄的顏色蓋掉。

那它是怎麼做到的呢?關鍵在這段程式:

private void setupStatusBarView(Context context, ViewGroup decorViewGroup) {
    mStatusBarTintView = new View(context);
    LayoutParams params = new LayoutParams(
            LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight());
    params.gravity = Gravity.TOP;
    if (mNavBarAvailable && !mConfig.isNavigationAtBottom()) {
        params.rightMargin = mConfig.getNavigationBarWidth();
    }
    mStatusBarTintView.setLayoutParams(params);
    mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
    mStatusBarTintView.setVisibility(View.GONE);
    decorViewGroup.addView(mStatusBarTintView);
}

邏輯非常直白:動態 new 一個 View,寬度 MATCH_PARENT、高度剛好等於系統狀態欄高度,設上背景色,最後塞進 decorViewGroup 這個容器裡。

@TargetApi(19)
public SystemBarTintManager(Activity activity) {
    Window win = activity.getWindow();
    // 取得 DecorView 根容器
    ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();
    ...
    if (mStatusBarAvailable) {
        // 往根布局裡補一個等高的 View
        setupStatusBarView(activity, decorViewGroup);
    }
    if (mNavBarAvailable) {
        setupNavBarView(activity, decorViewGroup);
    }
}

看出來了嗎?思路就是「狀態欄透明之後,往根布局補一塊等高的色塊」。至於這塊色塊是什麼顏色,完全由你決定。

做法二:用標題欄背景色去填滿狀態欄

把狀態欄設為半透明之後,新問題來了:原本狀態欄的位置會被下面的內容佔走。

這時候只要在 Activity 的根布局加上 android:fitsSystemWindows="true",狀態欄就會重新「露」出來而不被佔據——這個屬性的價值就在這裡。

接著我們手動取得狀態欄高度,給目標 View 設一個 padding-top。因為內容被頂上去剛好差這麼多,補回等高的 padding,該 View 的背景自然就填進了狀態欄區域,視覺上就「沉浸」了。

說白了就是:狀態欄半透明後一定會被佔位,那就讓下面的內容用 padding 把這段高度填回來。

@SuppressLint("InlinedApi")
public static void setImmerseLayout(Activity context, View view) {
    if (context == null || view == null) {
        return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window window = context.getWindow();
        window.setFlags(
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        int statusBarHeight = getStatusBarHeight(context.getBaseContext());
        view.setPadding(0, statusBarHeight, 0, 0);
    }
}

/** 取得狀態欄高度(像素),推薦用 Resource 反查,避免各機型偏差 */
public static int getStatusBarHeight(Context context) {
    int result = 0;
    int resourceId = context.getResources()
            .getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

有個細節千萬別忽略:標題欄的高度必須寫成 wrap_content。如果寫死具體高度,再加上 padding-top,標題欄裡的內容會被擠出去,顯示就不完整了。

實務上比較穩妥的寫法,是在原本的標題欄外面再包一層 <FrameLayout />,把背景色掛在這層容器上:

<FrameLayout
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/common_theme_color">

小結

沉浸式狀態欄本質上不神秘:靠 KitKat 的透明狀態欄 flag 打開局面,再用「補一個等高色塊」或「補一段等高 padding」這兩招之一收尾。理解原理之後,你可以自由搭配自家設計稿的配色與留白。歡迎留言交流你的實作心得。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved