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

Android 沉浸式狀態列的另外兩種思路(含側滑與輸入法相容)

編輯:關於 Android 程式設計

沉浸式狀態列並不新鮮,iOS 很早就有,Android 5.0 起也支援替狀態列上色。但由於一段時間內主流版本仍停留在 4.4,網路上常見的相容做法都是依賴「把狀態列變透明再上色」這個思路,核心程式碼如下:

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

    // 僅 API 19 以上有效
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        setTranslucentStatus(true);
    }

    // 替狀態列上色
    SystemBarTintManager tintManager = new SystemBarTintManager(this);
    tintManager.setStatusBarTintEnabled(true);
    tintManager.setStatusBarTintResource(R.color.statusbar_bg);
}

@TargetApi(19)
private void setTranslucentStatus(boolean on) {
    Window win = getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    if (on) {
        winParams.flags |= bits;
    } else {
        winParams.flags &= ~bits;
    }
    win.setAttributes(winParams);
}

再在根版面上補上兩個屬性即可:

android:fitsSystemWindows="true"
android:clipToPadding="false"

為什麼要找別的方案

這套寫法應付一般頁面沒問題,但碰上側滑選單這類設計就會出狀況。

參考效果:

在 4.4 的裝置上套用前述方案後,結果變成這樣:

除了視覺上不協調,還有另一個問題:使用抽屜選單或側滑返回時,狀態列沒有陰影層次。

期望的效果則是狀態列一起跟著滑動:

思路一:自肋骨況狀態列

在版面頂端預留一個佔位 View 當作狀態列。注意此時根版面不能加上 android:fitsSystemWindows="true",否則內容會被系統狀態列遮住、填不進去。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:orientation="vertical">

    <View
        android:id="@+id/status_bar"
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

接著在程式碼中依版本決定顯示與否,並設定實際高度:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View statusBar = findViewById(R.id.status_bar);
    setContentView(R.layout.activity_test);

    // API 19 以上才顯示,否則低版本會多出一塊空白
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        setTranslucentStatus(true);
        statusBar.setVisibility(View.VISIBLE);
        // 各家機型狀態列高度不同,需動態計算後設定 LayoutParams
    } else {
        statusBar.setVisibility(View.GONE);
    }
}

@TargetApi(19)
private void setTranslucentStatus(boolean on) {
    Window win = getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    if (on) {
        winParams.flags |= bits;
    } else {
        winParams.flags &= ~bits;
    }
    win.setAttributes(winParams);
}

這樣做同時解決了前述兩個問題:狀態列能自訂內容,也能隨版面一起滑動。

思路二:保留 fitsSystemWindows 並調整結構

思路一會遇到一個棘手狀況:設定了透明屬性的頁面,聊天輸入框不會被系統輸入法頂上去(打字時看不到輸入框)。而加上 android:fitsSystemWindows="true" 這個屬性後輸入框就正常了,但自肋骨況列又無法填到真正的狀態列位置。

加或不加都有明顯問題,於是有了第二種方案。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@android:color/holo_green_light"
    android:fitsSystemWindows="true"
    tools:context="com.saidtx.myapplication.TestActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/edit"
        android:background="@android:color/white">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="20dp"
                android:text="@string/previews" />

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</RelativeLayout>

關鍵就是這兩個屬性:

android:fitsSystemWindows="true"
android:background="@android:color/holo_green_light"

程式碼部分沿用網路上的通用方案即可,差別是不再需要自肋骨況列,也不必手動計算狀態列高度。要注意的是,子版面需各自設定背景色,否則會透出最外層的背景。

最終效果:

補充

兩個思路的取捨在於「控制力」與「系統行為相容性」。思路一完全掌控狀態列外觀,但要自行處理高度計算與輸入法;思路二順著系統的 fitsSystemWindows 機制走,相容性較好,卻犧牲了對狀態列內容的控制。

若專案的 minSdk 已高於 21,建議直接採用 Android 5.0 以後的 Window.setStatusBarColor() 與 SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN,不必再依賴 SystemBarTintManager 這類第三方類別,行為也更可預期。

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