让Android状态栏透明

android 4.4 (KITKAT)之后,Android Window支持了一些新属性,其中两个属性是

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION

即让状态栏和标题栏透明。试试这两个属性。

1.创建工程的时候选择NavigationView模板

when create android app chose one template

2.使用android studio自动生成的代码,在真机上运行起来是这样的

看到main.xml的布局是DrawerLayout + NavigationView

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

</android.support.v4.widget.DrawerLayout>

而MainActivity的Theme是NoActionBar,使用ToolBar代替actionbar

MainActivity在Manifest.xml里面的声明

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

MainActivity所使用的AppTheme.NoActionBar主题是下面这样子的,和上回说的直接使用Theme.AppCompat.Light.NoActionBar是一样的。

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true"
    tools:context="jean.test.testtransparentstatubar.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

在5.x上,代码运行起来后是这样子的:

drawerlayout in 5.x

在5.x上,drawerlayout打开之后是这样子的:

5.x open drawerlayout

在4.4的真机上,是这样子的,由于是华为系统,系统主题状态栏颜色和此处的colorPrimaryDark很接近,所以为了区分,准备换一个colorPrimaryDark,换成红色#FF0000。

在5.x下是这样的

change colorprimary to red

open drawerlayout see statusbar color

在4.4下是这样的

4.4 open drawerlayout

4.4 close drawerlayout

3.把colorPrimaryDark变成和colorPrimary一样,方便查看TRANSLUCENT的效果。使状态栏和标题栏透明,在代码里面加上这两句,看看效果如下图

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS 在 4.4 和 5.x 上表现不同,4.4 是一层渐变的遮罩层;5.x 以上是一条半透明的遮罩层。

在4.4里面的效果如下。可以看到状态栏(Status Bar)和下面的菜单按键导航栏(Navigation Bar)是这样的。感觉是白色的渐变😂并没有透明。在4.4上的样子显然不是我们想要的,而且感觉布局还自动往上和往下拉伸了!!!!!!😂😂😂😂

4.4 set translucent status and navigation -- drawerlayout closed

4.4 set translucent status and navigation -- drawerlayout opened

在5.x的效果是这样的,可以看到StatusBar上和navigationBar的确是半透明的遮罩。

下面这图这是5.x没设置Translucent的时候,colorPrimary和colorPrimarDark颜色一样的时候

5.x colorPrimary equals colorPrimaryDark

下面这图这是5.x设置了Translucent的时候的样式,看到的确是变成了半透明的遮罩

5.x colorPrimary equals colorPrimaryDark but set translucent status and navigation


说一下**android:fitsSystemWindows="true"**这个属性

当把布局文件里面的所有的android:fitsSystemWindows="true"都删掉之后(包括DrawerLayout、CoordinatorLayout、NavigationView),是这样子的。发现app的内容被向上拉伸到StatusBar底下和被向下拉伸到NavigationBar底下。

既然4.4上设置Translucent的结果并不是我们想要的,试一下假如把app的内容扩展到系统栏里面是什么样子,白色渐变的系统栏会不会呈现app控件的颜色?

在4.4上去除所有android:fitsSystemWindows="true",结果是这样的。DrawerLayout不打开的时候,勉强还能忍受一下状态栏覆盖在标题栏上,DrawerLayout一开打就不好了😂

4.4 remove all flag of fitSystemWindow in layout.xml--drawerlayout closed

4.4 remove all flag of fitSystemWindow in layout.xml--drawerlayout opened

在5.x上是这样的

5.x remove all flag of fitSytemWindow in layout.xml -- drawerlayout closed

5.x remove all flag of fitSytemWindow in layout.xml -- drawerlayout opened


为了解决4.4上想要设置状态栏和标题栏一致的问题,可以这样:

1.在代码里面设置translucent statu和translucent navigation

2.去除根布局的android:fitsSystemWindows="true"属性

3.使用第三方库SystemBarTintManager第三方库设置statusbar的颜色.(不过这个库现在作者已经不维护了)

此处设置statusbar的颜色为绿色,效果如下面两张图所示。

            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(Color.parseColor("#00FF00"));
            tintManager.setStatusBarTintEnabled(true);

4.4 use SystemBarTintManager change StatusBar color

4.4 use SystemBarTintManager change StatusBar color--drawerlayout opened

实际使用的时候把statusbar设置成和标题栏一样的颜色。但是drawerlayout打开的时候还是看到navigationview的颜色和statusbar的颜色不一样,就再把NavigationView的headerView的颜色也设置成一样,比如都设置成colorPrimary的颜色。效果如下:

4.4 set StatusBar color equals colorPrimary

4.4 set Navigation header view color equals colorPrimary

Show Comments