1. 程式人生 > >Android App 沉浸式狀態列解決方案

Android App 沉浸式狀態列解決方案


伴隨著 Android 5.0 釋出的 Material Design,讓 Android 應用告別了以前的工程師審美,迎來了全新的介面,靈動的互動,也讓越來越多的 App 開始遵從 material design 設計原則,不再是以前拿著iOS設計稿,做著Android開發。本文就其中的沉浸式狀態列這一特性,描述其相容到4.4的實現,以及一些使用中的小細節。

前言

在4.4之前狀態列一直是黑色的,在4.4中帶來了 windowTranslucentStatus 這一特性,因此可以實現給狀態列設定顏色,如下圖所示,狀態列顏色不再是黑色,而是可以定製的顏色。

國內將狀態列變色叫做沉浸式狀態列,時間久了,叫的人多了,大家就不再深究,默認了這種叫法。

需要解決的問題

  1. 4.4及其以上都是可以實現沉浸式狀態列效果的,5.0及其以上可以直接在主題中設定顏色,或者呼叫 Window 類中的 setStatusBarColor(int color) 來實現,這兩種方式在5.0上都比較簡單,但是如何相容到4.4呢?
  2. 圖片背景的頁面,怎樣讓狀態列透明或者半透明(效果如下)? 
  3. 使用 DrawerLayout 時,主介面實現沉浸狀態列同時,怎樣保證抽屜檢視也能延伸到狀態列(如下圖所示),且相容到4.4? 

以上就是本文要解決的問題,下面給出解決方案。

解決方案

1. 給狀態列設定顏色

思路是:

  • 先設定狀態列透明屬性;
  • 給根佈局加上一個和狀態列一樣大小的矩形View(色塊),新增到頂上;
  • 然後設定根佈局的 FitsSystemWindows 屬性為 true,此時根佈局會延伸到狀態列,處在狀態列位置的就是之前新增的色塊,這樣就給狀態列設定上顏色了。

程式碼如下:

    /** * 設定狀態列顏色 * * @param activity 需要設定的activity * @param color 狀態列顏色值 */
    public static void setColor(Activity activity, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 設定狀態列透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 生成一個狀態列大小的矩形
            View statusView = createStatusView(activity, color);
            // 新增 statusView 到佈局中
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            decorView.addView(statusView);
            // 設定根佈局的引數
            ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipToPadding(true);
        }
    }

其中生成狀態列一樣大小的矩形色塊的程式碼如下:

    /** * 生成一個和狀態列大小相同的矩形條 * * @param activity 需要設定的activity * @param color 狀態列顏色值 * @return 狀態列矩形條 */
    private static View createStatusView(Activity activity, int color) {
        // 獲得狀態列高度
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);

        // 繪製一個和狀態列一樣高的矩形
        View statusView = new View(activity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                statusBarHeight);
        statusView.setLayoutParams(params);
        statusView.setBackgroundColor(color);
        return statusView;
    }

在 setContentView() 之後呼叫 setColor(Activity activity, int color) 方法即可。

2. 圖片作背景時,狀態列透明

這個實現比較簡單,根佈局背景設定為圖片,然後新增狀態列透明 Flag, 然後設定根佈局的FitsSystemWindows 屬性為 true 即可。程式碼如下:

    /** * 使狀態列透明 * <p> * 適用於圖片作為背景的介面,此時需要圖片填充到狀態列 * * @param activity 需要設定的activity */
    public static void setTranslucent(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 設定狀態列透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 設定根佈局的引數
            ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipToPadding(true);
        }
    }

同樣的,在 setContentView() 之後呼叫 setTranslucent(Activity activity) 方法即可。

3. 使用 DrawerLayout 時的特殊處理

注意點:

  • 使用 DrawerLayout 時,此時不能再對根佈局,即 DrawerLayout 進行設定,而要針對 DrawerLayout 的內容佈局進行設定,即抽屜之外的另一個佈局。

    如下是一個典型的 DrawerLayout 的佈局,其內容佈局即 FrameLayout,我們需要對FrameLayout 進行仿狀態列色塊的新增、FitsSystemWindows 屬性的設定。

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <LinearLayout
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
        </LinearLayout>
    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>
  • 還有一個需要注意的設定抽屜佈局(Drawer)的 FitsSystemWindows 屬性為 false,即上面佈局中的 NavigationView

解決方案

  • DrawerLayout 狀態列變色

    程式碼如下:

    /** * 為DrawerLayout 佈局設定狀態列變色 * * @param activity 需要設定的activity * @param drawerLayout DrawerLayout * @param color 狀態列顏色值 */
    public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 生成一個狀態列大小的矩形
            View statusBarView = createStatusBarView(activity, color);
            // 新增 statusBarView 到佈局中
            ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
            contentLayout.addView(statusBarView, 0);
            // 內容佈局不是 LinearLayout 時,設定padding top
            if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
                contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
            }
            // 設定屬性
            ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
            drawerLayout.setFitsSystemWindows(false);
            contentLayout.setFitsSystemWindows(false);
            contentLayout.setClipToPadding(true);
            drawer.setFitsSystemWindows(false);
        }
    }

需要注意的是,DrawerLayout 的佈局只能包含兩個直接子佈局,一個是內容佈局,一個是抽屜佈局,結構如前面的示例佈局所示,如果內容佈局的根佈局如果不是 LinearLayout 需要對其子佈局設定padding top值,否則仿狀態列色塊會被遮擋在最下面,佈局內容延伸到狀態列,如下圖所示:

(ps:就上圖中的問題,目前的解決方案感覺並不是很好,如果你有更好的解決方案,請告訴我~)

  • DrawerLayout 狀態列透明
    /** * 為 DrawerLayout 佈局設定狀態列透明 * * @param activity 需要設定的activity * @param drawerLayout DrawerLayout */
    public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 設定狀態列透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 設定內容佈局屬性
            ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
            contentLayout.setFitsSystemWindows(true);
            contentLayout.setClipToPadding(true);
            // 設定抽屜佈局屬性
            ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1);
            vg.setFitsSystemWindows(false);
            // 設定 DrawerLayout 屬性
            drawerLayout.setFitsSystemWindows(false);
        }
    }

同樣的,在 setContentView() 之後呼叫上述解決方案中的方法即可。

在專案中使用

在專案中推薦這樣使用,在 BaseActivity 中重寫 setContentView(int layoutResID) 方法,新建一個 setStatusBarColor()方法,全域性設定狀態列顏色,因為一般 App 大部分介面狀態列都是主題色。

public class BaseActivity extends AppCompatActivity {

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        setStatusBarColor();
    }

    protected void setStatusBar() {
        StatusBarUtils.setColor(this, getResources().getColor(R.color.colorPrimary));
    }

}

當子類 Activity 的狀態列需要特殊處理時,比如設定不同的顏色,或者設定圖片為背景時,重寫父類的 setStatusBarColor() 方法即可,例如:

public class ImageStatusBarActivity extends BaseActivity {

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

    @Override
    protected void setStatusBar() {
        StatusBarUtils.setTranslucent(this);
    }

對 DrawerLayout 佈局使用時,需要注意一點,因為方法是在 setContentView() 之後立即呼叫的,所以傳進來的 DrawerLayout 要通過 findViewById() 傳進來。如果傳入在setContentView() 之後通過 findViewById() 得到的 DrawerLayout, 則會造成空指標異常。

 StatusBarUtils.setColorForDrawerLayout(this, (DrawerLayout) findViewById(R.id.drawer_layout), getResources()
                .getColor(R.color.colorPrimary));

原始碼和Demo下載

效果在前文中都有截圖,就不多放了。

如文章中有疏漏的地方,請聯絡我或在評論裡告知。