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

Android 狀態欄透明化與著色:三種實作方案與場景對照

編輯:關於 Android 程式設計

專案上遇到兩個相關需求:狀態欄要透明,且當使用者拖動、頂部內容滾動時,標題欄顏色要能跟著變化。這類效果常被稱為「沉浸式狀態欄」(這個叫法網路上有不少討論,這裡統一稱之為狀態欄著色,比較貼近實作內容)。

從 Android 4.4 開始才支援狀態欄著色,5.0 之後 API 更完整,可以直接在主題裡寫 android:colorPrimaryDark,或用 getWindow().setStatusBarColor(color) 設定。但 4.4 以上的裝置仍佔相當比例,因此需要一套向下相容的做法。

方案一:讓版面延伸到狀態欄下方(推薦)

這個方案不做 fitsSystemWindows,而是讓內容填滿狀態欄區域,再自行安排頂部留白。彈性最高,特別適合「狀態欄顏色需跟隨 Fragment 切換」這類場景。

步驟 1:把狀態欄變透明

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // 5.0 及以上
    View decorView = getWindow().getDecorView();
    int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
               | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
    decorView.setSystemUiVisibility(option);
    getWindow().setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    // 4.4 ~ 5.0
    WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
    localLayoutParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                            | localLayoutParams.flags;
}

放在 Activity 或基類中執行即可。除了程式碼,也可以用主題達成同樣效果,在 values、values-v19、values-v21 三個目錄分別定義:

<!-- values -->
<style name="TranslucentTheme" parent="AppTheme" />

<!-- values-v19 -->
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">false</item>
</style>

<!-- values-v21 -->
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">false</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

幫 Activity 或 Application 掛上這個主題即可。兩種方式依需求挑一種,不衝突。

步驟 2:給狀態欄上色

透明化之後直接執行,會看到版面頂到狀態欄底下。這正是我們要的——接下來只要讓頂部元件補上等高的留白,顏色自然就上去了。

先在 values 與 values-v19 各定義一個高度:

<!-- values -->
<dimen name="padding_top">0dp</dimen>

<!-- values-v19 -->
<dimen name="padding_top">25dp</dimen>

這裡的 25dp 在部分客製系統上會有誤差,若要求精準,建議改用 getStatusBarHeight() 動態計算並以程式設定 padding。

場景 2.1:頂部是 Toolbar(或自定義 title)

最常見的情況是狀態欄與 Toolbar 同色。既然版面已經延伸到狀態欄,只要給 Toolbar 加上一個狀態欄高度的 top padding:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/padding_top"
    app:contentInsetStart="0dp" />

自定義 title 的情況經測試也可行,做法相同。

場景 2.2:DrawerLayout + NavigationView + Toolbar

先給 Toolbar 加上 top padding,在 4.4 上看,NavigationView 並沒有延伸到狀態欄。這時在 5.0 以下補一段設定:

private void navViewToTop() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        mDrawerLayout.setFitsSystemWindows(true);
        mDrawerLayout.setClipToPadding(false);
    }
}

場景 2.3:頂部是一張大圖

這是最省事的情況,因為版面已經延伸,圖片自然會填到狀態欄底下,不需要任何額外處理:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@mipmap/top_image" />

</LinearLayout>

場景 2.4:底部 Tab 切換多個 Fragment

這種頁面常有「每個 Fragment 頂部顏色不同」的需求。原理與 2.1 相同——每個 Fragment 用自己的 title 並補上 paddingTop,顏色就各自獨立了。以 FragmentOne 為例:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff9900"
        android:paddingTop="@dimen/padding_top">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="fragment one" />
    </LinearLayout>

</LinearLayout>

FragmentTwo 結構相同,只把背景換成藍色。切換效果:

顏色跟隨切換、沒有延遲感。這個做法比早期常見的「在 Activity 層手動切換狀態欄顏色」自然得多,後者容易出現過渡不順的問題。

方案二:fitsSystemWindows + 覆蓋一層 View

這次反過來,用 android:fitsSystemWindows="true" 讓版面不要延伸,狀態欄保持透明,再手動塞一個等高同寬的 View 去蓋住它。

第一步一樣先透明化,第二步在根布局加上屬性:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">
</LinearLayout>

第三步,動態建立狀態欄 View 並加入內容區:

private void addStatusBarView() {
    View view = new View(this);
    view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this));

    ViewGroup decorView = findViewById(android.R.id.content);
    decorView.addView(view, params);
}

原理很簡單,缺點是每個頁面都得額外寫一次這段程式碼。

方案三:fitsSystemWindows + 根布局上色

同樣使用 android:fitsSystemWindows="true",但改成直接把根布局的背景設成狀態欄顏色。因為根布局整塊都被染色,內部需要多包一層 Layout 來指定真正的內容背景(例如白色),否則整個畫面都會變成狀態欄色。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff9900"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical">
        <!-- 實際內容 -->
    </LinearLayout>

</RelativeLayout>

看起來乾淨,但版面嵌套多一層。若專案有幾十個頁面,一頁一頁改寫的同時還要留意嵌套層級會不會影響效能與滾動行為,成本並不低。

怎麼選

三個方案各有適用範圍:

  • 方案一:彈性最好,適合頂部需要延伸到狀態欄的設計(大圖、漸層、跟隨 Fragment 變色),改動集中在布局的 padding,不用寫額外程式碼。個人最推薦。
  • 方案二:適合沿用既有 fitsSystemWindows 版面、只想補一條色帶的情況,但需要重複寫程式碼。
  • 方案三:最省事於單頁,但嵌套層數增加,多頁專案維護成本高。

另外補充一點:4.4 的 FLAG_TRANSLUCENT_STATUS 是「模擬透明」,系統會在半透明狀態欄上蓋一層灰階遮罩,無法做到真正的全透明或自訂顏色。若設計稿要求精準還原,5.0 以上的 setStatusBarColor() 才是完整解法,4.4 只能當作降級方案對待。

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