Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄
一、概述
近期注意到QQ新版使用了沉浸式狀態(tài)欄,ok,先聲明一下效果圖:

恩,接下來正題。
首先只有大于等于4.4版本支持這個(gè)半透明狀態(tài)欄的效果,但是4.4和5.0的顯示效果有一定的差異,所有本文內(nèi)容為:
1.如何實(shí)現(xiàn)半透明狀態(tài)欄效果在大于4.4版本之上。
2.如何讓4.4的效果與5.0的效果盡可能一致。
先貼下模擬器效果圖,以便和實(shí)現(xiàn)過程中做下對(duì)比
4.4 模擬器

5.x 真機(jī)

二、實(shí)現(xiàn)半透明狀態(tài)欄
因?yàn)楸纠褂昧薔avigationView,所以布局代碼稍多,當(dāng)然如果你不需要,可以自己進(jìn)行篩減。
注意引入相關(guān)依賴:
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、colors.xml 和 styles.xml
首先我們定義幾個(gè)顏色:
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>
下面定義幾個(gè)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>
values-v19
<resources>
<style name="AppTheme" parent="@style/BaseAppTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
ok,這個(gè)沒撒說的。注意我們的主題是基于NoActionBar的,Android:windowTranslucentStatus這個(gè)屬性是v19開始引入的。
2、布局文件
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>
DrawerLayout內(nèi)部一個(gè)LinearLayout作為內(nèi)容區(qū)域,一個(gè)NavigationView作為菜單。
注意下Toolbar的高度設(shè)置為wrap_content。
然后我們的NavigationView中又依賴一個(gè)布局文件和一個(gè)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>
menu的文件就不貼了,更加詳細(xì)的可以去參考Android 自己實(shí)現(xiàn) NavigationView [Design Support Library(1)]。
大體看完布局文件以后,有幾個(gè)點(diǎn)要特別注意:
(1)ToolBar高度設(shè)置為wrap_content
(2)ToolBar添加屬性android:fitsSystemWindows="true"
(3)header_just_username.xml的跟布局RelativeLayout,添加屬性android:fitsSystemWindows="true"
(4)android:fitsSystemWindows這個(gè)屬性,主要是通過調(diào)整當(dāng)前設(shè)置這個(gè)屬性的view的padding去為我們的status_bar留下空間。
根據(jù)上面的解釋,如果你不寫,那么狀態(tài)欄和Toolbar就會(huì)有擠一塊的感覺了,類似會(huì)這樣:

ok,最后看下代碼。
3、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);
}
}
沒撒說的,就是setSupportActionBar。
那么現(xiàn)在4.4的效果圖是:

其實(shí)還不錯(cuò),有個(gè)漸變的效果。
現(xiàn)在5.x的效果:

可以看到5.x默認(rèn)并非是一個(gè)漸變的效果,類似是一個(gè)深一點(diǎn)的顏色。
在看看我們md的規(guī)范:

狀態(tài)欄應(yīng)該是一個(gè)比Toolbar背景色,稍微深一點(diǎn)的顏色。
這么看來,我們還是有必要去為4.4做點(diǎn)適配工作,讓其竟可能和5.x顯示效果一致,或者說盡可能符合md的規(guī)范。
三、調(diào)整4.4的顯示方案
那么問題來了?如何做呢?
咱們這么看,4.4之后加入windowTranslucentStatus的屬性之后,也就是我們可以用到狀態(tài)欄的區(qū)域了。
既然我們可以用到這塊區(qū)域,那么我們只要在根布局去設(shè)置一個(gè)與狀態(tài)欄等高的View,設(shè)置背景色為我們期望的顏色就可以了。
于是有了以下的代碼:
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;
}
}
代碼的思路很簡(jiǎn)單,根據(jù)Activity找到android.R.content,在其中添加一個(gè)View(高度為statusbarHeight,背景色為我們?cè)O(shè)置的顏色,默認(rèn)為半透明的黑色)。
那么只需要在Activity里面去寫上:
StatusBarCompat.compat(this);
就可以了。
如果你希望自己設(shè)置狀態(tài)看顏色,那么就用這個(gè)方法:
StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
這樣的話我們就解決了4.4到5.x的適配問題,一行代碼解決,感覺還是不錯(cuò)的。
最后提一下,對(duì)于5.0由于提供了setStatusBarColor去設(shè)置狀態(tài)欄顏色,但是這個(gè)方法不能在主題中設(shè)置windowTranslucentStatus屬性。所以,可以編寫一個(gè)value-v21文件夾,里面styles.xml寫入:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="@style/BaseAppTheme"> </style> </resources>
其實(shí)就是不要有windowTranslucentStatus屬性。
接下來,對(duì)于默認(rèn)的效果就不測(cè)試了,參考上面的效果圖。
我們測(cè)試個(gè)設(shè)置狀態(tài)欄顏色的,我們這里設(shè)置個(gè)紅色。
4.4模擬器

5.x 真機(jī)

ok,這樣就結(jié)束啦~~
相關(guān)文章
Android實(shí)現(xiàn)自動(dòng)變換大小的組件ViewPager2
這篇文章主要介紹了Android實(shí)現(xiàn)自動(dòng)變換大小的組件ViewPager2,ViewPager2最顯著的特點(diǎn)是基于RecyclerView實(shí)現(xiàn),RecyclerView是目前Android端最成熟的AdapterView解決方案2023-03-03
Android應(yīng)用 坐標(biāo)系詳細(xì)介紹
這篇文章主要介紹了 Android 坐標(biāo)系的相關(guān)資料,本文對(duì)Android 坐標(biāo)系的知識(shí)做了詳細(xì)解讀,需要的朋友可以參考下2016-11-11
Android 同時(shí)setTag兩次保存多種值的示例代碼
這篇文章主要介紹了Android 同時(shí)setTag兩次保存多種值的示例代碼,需要的朋友可以參考下2017-02-02
Android開發(fā)教程之shape和selector的結(jié)合使用
shape和selector是Android UI設(shè)計(jì)中經(jīng)常用到的,比如我們要自定義一個(gè)圓角Button,點(diǎn)擊Button有些效果的變化,就要用到shape和selector,接下來通過本文給大家介紹Android開發(fā)教程之shape和selector的結(jié)合使用,感興趣的朋友一起學(xué)習(xí)吧2016-01-01
Android中利用Xposed框架實(shí)現(xiàn)攔截系統(tǒng)方法
這篇文章主要介紹了Android中利用Xposed框架實(shí)現(xiàn)攔截系統(tǒng)方法的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android開發(fā)之圖形圖像與動(dòng)畫(四)AnimationListener簡(jiǎn)介
就像Button控件有監(jiān)聽器一樣,動(dòng)畫效果也有監(jiān)聽器,只需要實(shí)現(xiàn)AnimationListener就可以實(shí)現(xiàn)對(duì)動(dòng)畫效果的監(jiān)聽,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01
Android開發(fā)實(shí)現(xiàn)讀取Assets下文件及文件寫入存儲(chǔ)卡的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)讀取Assets下文件及文件寫入存儲(chǔ)卡的方法,涉及Android文件與目錄的讀取、寫入、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
用原生VideoView進(jìn)行全屏播放時(shí)的問題
本篇文章主要介紹了用原生VideoView進(jìn)行全屏播放時(shí)的問題,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01

