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

Android 沉浸式狀態欄:讓狀態欄與頂部元件同色

編輯:關於 Android 程式設計

這個特性從 Android 4.4 開始支援,最低需要 API 19。做法出乎意料地簡單,但真正要用得好看,還有幾個細節要處理。

兩行關鍵程式碼

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 透明狀態欄
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        // 透明導航欄
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
}

加上這兩行,系統的狀態列與導航列就變成半透明,應用內容會延伸到下方。效果如下:

第一個問題:內容被狀態欄壓住

版面是一個很單純的垂直 LinearLayout:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#009959" />

    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="#ff669d" />
</LinearLayout>

但一旦在綠色的 TextView 裡塞入文字:

文字直接跟狀態欄重疊了。因為內容已經向上延伸,系統不會自動幫你避開。

解法一:加 fitsSystemWindows(但不夠好)

android:fitsSystemWindows="true"
android:clipToPadding="true"
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:clipToPadding="true"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#009959" />

    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="#ff669d" />
</LinearLayout>

加上之後,界面仍然是沉浸式的,狀態欄區域也不再重疊,就像自動補上了 padding:

但問題來了:狀態欄顯示的是最外層 LinearLayout 的白色背景,綠色 TextView 與粉色按鈕整體被往下推。這顯然不是我們要的——我們希望狀態欄的顏色與頂部元件一致。

正解:把屬性移到頂部元件上

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#009959"
        android:clipToPadding="true"
        android:fitsSystemWindows="true"
        android:text="你好,請問你有男朋友嗎?" />

    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="#ff669d" />
</LinearLayout>

把這兩行屬性從外層容器挪到綠色 TextView 身上,就得到我們想要的結果:狀態欄變成與頂部元件相同的綠色,而文字內容自動下移、不再被遮壓。

原理與實務建議

fitsSystemWindows="true" 的行為是「把系統 UI 的尺寸當成 padding 套在自己身上」。放在外層,被推開的是整個版面;放在頂部元件,被推開的只是那個元件自己的內容,背景色則自然填滿狀態欄區域。這就是兩種寫法視覺差異的來源。

若要進一步做到 Android 5.0 之後那種「狀態欄半透明並帶有漸層」的效果,改用 setStatusBarColor() 搭配 Window.setStatusBarColor() 會更直接;而 API 21 以後也提供了 SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 等更精細的 flag 組合。這裡的 addFlags 寫法勝在直觀、相容性廣,適合快速把舊專案改造成沉浸式。

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