Android 沉浸式狀態列攻略 讓你的狀態列變色吧
一、概述
近期注意到QQ新版使用了沉浸式狀態列,ok,先宣告一下:本篇部落格效果下圖:
關於這個狀態列變色到底叫「Immersive Mode」/「Translucent Bars」有興趣可以去 為什麼在國內會有很多使用者把 「透明欄」(Translucent Bars)稱作 「沉浸式頂欄」?上面瞭解瞭解,請勿指點我說的博文標題起得不對,thx。
恩,接下來正題。
首先只有大於等於4.4版本支援這個半透明狀態列的效果,但是4.4和5.0的顯示效果有一定的差異,所有本篇博文內容為:
- 如何實現半透明狀態列效果在大於4.4版本之上。
- 如何讓4.4的效果與5.0的效果儘可能一致。
看了不少參考文章,都介紹到這個庫,大家可以瞭解:
不過本篇博文並未基於此庫,自己想了個hack,對於此庫原始碼有空再看了。
二、效果圖
先貼下效果圖,以便和實現過程中做下對比
- 4.4 模擬器
- 5.x 真機
[new]貼個如果頂部是圖片的效果圖,其實是一樣的,為了方便我就放側欄的頂部了。
稍等,csdn圖片伺服器異常…
ok,有了效果圖之後就開始看實現了。
三、實現半透明狀態列
因為本例使用了NavigationView,所以佈局程式碼稍多,當然如果你不需要,可以自己進行篩減。
注意引入相關依賴:
compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.android.support:support-v4:22.2.1' compile 'com.android.support:design:22.2.0'
- 1
- 2
- 3
(一)colors.xml 和 styles.xml
首先我們定義幾個顏色:
res/values/color.xml
<?xml version="1.0" encoding="utf-8"?><resources> <color name="primary">#FF03A9F4</color> <color name="primary_dark">#FF0288D1</color> <color name="status_bar_color">@color/primary_dark</color> </resources>
- 1
- 2
- 3
- 4
- 5
- 6
下面定義幾個styles.xml
注意資料夾的路徑:
values/styles.xml
<resources> <style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primary_dark</item> <item name="colorAccent">#FF4081</item> </style> <!-- Base application theme. --> <style name="AppTheme" parent="@style/BaseAppTheme"></style></resources>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
values-v19
<resources> <style name="AppTheme" parent="@style/BaseAppTheme"> <item name="android:windowTranslucentStatus">true</item> </style></resources>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
ok,這個沒撒說的。注意我們的主題是基於NoActionBar的,android:windowTranslucentStatus
這個屬性是v19開始引入的。
(二)佈局檔案
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/id_main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/id_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:fitsSystemWindows="true" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> <TextView android:id="@+id/id_tv_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="HelloWorld" android:textSize="30sp"/> </LinearLayout> <android.support.design.widget.NavigationView android:id="@+id/id_nv_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/header_just_username" app:menu="@menu/menu_drawer" /></android.support.v4.widget.DrawerLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
DrawerLayout內部一個LinearLayout作為內容區域,一個NavigationView作為選單。 注意下Toolbar的高度設定為wrap_content。
然後我們的NavigationView中又依賴一個佈局檔案和一個menu的檔案。
header_just_username.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="192dp" android:background="?attr/colorPrimaryDark" android:orientation="vertical" android:padding="16dp" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <TextView android:id="@+id/id_link" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="16dp" android:text="http://blog.csdn.net/lmj623565791"/> <TextView android:id="@+id/id_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/id_link" android:text="Zhang Hongyang"/> <ImageView android:layout_width="72dp" android:layout_height="72dp" android:layout_above="@id/id_username" android:layout_marginBottom="16dp" android:src="@mipmap/ic_launcher"/></RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
大體看完佈局檔案以後,有幾個點要特別注意:
- ToolBar高度設定為
wrap_content
- ToolBar新增屬性
android:fitsSystemWindows="true"
- header_just_username.xml的跟佈局RelativeLayout,新增屬性
android:fitsSystemWindows="true"
android:fitsSystemWindows這個屬性,主要是通過調整當前設定這個屬性的view的padding去為我們的status_bar留下空間。
根據上面的解釋,如果你不寫,那麼狀態列和Toolbar就會有擠一塊的感覺了,類似會這樣:
ok,最後看下程式碼。
(三)Activity的程式碼
package com.zhy.colorfulstatusbar;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar); setSupportActionBar(toolbar); //StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color)); //StatusBarCompat.compat(this); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
沒撒說的,就是setSupportActionBar。
那麼現在4.4的效果圖是:
其實還不錯,有個漸變的效果。
現在5.x的效果:
可以看到5.x預設並非是一個漸變的效果,類似是一個深一點的顏色。
在看看我們md的規範
狀態列應該是一個比Toolbar背景色,稍微深一點的顏色。
這麼看來,我們還是有必要去為4.4做點適配工作,讓其竟可能和5.x顯示效果一致,或者說盡可能符合md的規範。
四、調整4.4的顯示方案
那麼問題來了?如何做呢?
咱們這麼看,4.4之後加入windowTranslucentStatus
的屬性之後,也就是我們可以用到狀態列的區域了。
既然我們可以用到這塊區域,那麼我們只要在根佈局去設定一個與狀態列等高的View,設定背景色為我們期望的顏色就可以了。
於是有了以下的程式碼:
package com.zhy.colorfulstatusbar;import android.annotation.TargetApi;import android.app.Activity;import android.content.Context;import android.graphics.Color;import android.os.Build;import android.view.View;import android.view.ViewGroup;/** * Created by zhy on 15/9/21. */public class StatusBarCompat{ private static final int INVALID_VAL = -1; private static final int COLOR_DEFAULT = Color.parseColor("#20000000"); @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static void compat(Activity activity, int statusColor) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (statusColor != INVALID_VAL) { activity.getWindow().setStatusBarColor(statusColor); } return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { int color = COLOR_DEFAULT; ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); if (statusColor != INVALID_VAL) { color = statusColor; } View statusBarView = new View(activity); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); statusBarView.setBackgroundColor(color); contentView.addView(statusBarView, lp); } } public static void compat(Activity activity) { compat(activity, INVALID_VAL); } 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; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
程式碼的思路很簡單,根據Activity找到android.R.content,在其中新增一個View(高度為statusbarHeight,背景色為我們設定的顏色,預設為半透明的黑色)。
那麼只需要在Activity裡面去寫上:
StatusBarCompat.compat(this);
- 1
就可以了。
如果你希望自己設定狀態看顏色,那麼就用這個方法:
StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
- 1
這樣的話我們就解決了4.4到5.x的適配問題,一行程式碼解決,感覺還是不錯的。
最後提一下,對於5.0由於提供了setStatusBarColor去設定狀態列顏色,但是這個方法不能在主題中設定windowTranslucentStatus
屬性。所以,可以編寫一個value-v21資料夾,裡面styles.xml寫入:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="@style/BaseAppTheme"></style></resources>
- 1
- 2
- 3
- 4
- 5
- 6
其實就是不要有windowTranslucentStatus屬性。
接下來,對於預設的效果就不測試了,參考上面的效果圖。
我們測試個設定狀態列顏色的,我們這裡設定個紅色。
- 4.4 模擬器
- 5.x 真機
ok,這樣就結束啦~~
群號:463081660,歡迎入群
微信公眾號:hongyangAndroid (歡迎關注,第一時間推送博文資訊)