Android沉浸式状态栏

关于Android沉浸式状态栏, 网上已经有很多开源库, 虽然开源库可以解决某些特定布局下的沉浸式状态栏问题, 但是遇到比较特殊的布局就无法解决了, 所以了解一下沉浸式状态栏如何实现是有必要的. 无论布局怎么变,我们根据一定的规律去实现. 总是可以找到合适的方法.

  1. 设置默认主题
  2. 相关属性讲解
  3. 各种布局下沉浸式状态栏如何做

一. Activity主题设置

这里只是简单设置一下Android所有版本的默认主题.其他与Api19+以及Api21+相关的主题属性,我们在代码里面设置.

1.设置Activity的默认主题

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--没有标题-->
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <!--窗口透明-->
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@color/white</item>
  </style>
  

2. 设置主题之后默认如下图

4.4效果 7.0效果
默认4.4-w340
默认4.4-w340
默认7.0-w340
默认7.0-w340

二. 状态栏相关属性解析

1. flag类型属性

属性 说明 api
WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS Api21以上设置这个标记才能更改状态栏,默认将绘制colorPrimaryDark颜色的状态栏,如果需要和Api19以前保持一致需要清除这个标记. Api21+
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS 设置Activity的内容是否延伸到状态栏下层,并使得状态栏透明(Api21+半透明),此时Api21+ getWindow().setStatusBarColor(Color.RED)设置的状态栏颜色值不会显示.设置根布局的属android:fitsSystemWindows="true"将使得根据局里面的内容与顶部的间隔距离为状态栏高度(不被状态栏挡住),根布局仍然在状态栏下层. Api19+

2. SystemUiVisibility方法参数

参数说明getActivity().getWindow().getDecorView().setSystemUiVisibility;

属性 说明
* View.SYSTEM_UI_FLAG_VISIBLE: 显示状态栏,Activity不全屏显示(恢复到有状态栏的正常情况)。
* View.INVISIBLE: 隐藏状态栏,但是位置仍然空出来.
* View.SYSTEM_UI_FLAG_FULLSCREEN: Activity全屏显示,且状态栏被隐藏覆盖掉。(效果不一致)
* View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN: Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住。
* View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION: 效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
* View.SYSTEM_UI_LAYOUT_FLAGS: 效果同View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
* View.SYSTEM_UI_FLAG_HIDE_NAVIGATION: 隐藏虚拟按键(导航栏)。有些手机会用虚拟按键来代替物理按键。
* View.SYSTEM_UI_FLAG_LOW_PROFILE: 状态栏显示处于低能显示状态(low profile模式),状态栏上一些图标显示会被隐藏。
* View.SYSTEM_UI_FLAG_IMMERSIVE
* View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY 某个标志被手动清空之后一段时间重新设置.
* View.SYSTEM_UI_FLAG_LAYOUT_STABLE

二. 各种布局下沉浸式状态栏如何做

1. 基本布局沉浸式状态栏设置,效果图如下(BasicFragment.java|CustomFragment.java)

4.4效果 7.0效果
4.4效果-w340
4.4效果-w340
默认7.0-w340
默认7.0-w340

在此为了兼容Android Api19+所有系统,所以我们需要使用自定义的状态栏来实现.也就是说状态栏,标题栏都由Activity的布局文件指定.
首先看一下界面层, 最底下一层是我们的内容区域,内容区域上面是自定义的状态栏+标题栏.自定义状态栏上面一层是系统UI(如电池图标,时间,网络等).
在此我没需要设置系统的状态栏为透明,并使得自定义内容区域延伸到状态栏下方,以便使自定义状态栏的颜色显示出来.通过设置内容区域的padding属性使得内容不被自定义状态栏挡住.
具体方法代码如下

/**
   * {@link Build.VERSION_CODES#KITKAT}以上系统调用此方法,可以是状态栏透明,
   * 并使得Activity内容显示在状态栏下层,内容被状态栏覆盖
   * 设置内容显示到状态栏下层,并使状态栏透明
   */
  public void setTransparentForWindow() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      getWindow().setStatusBarColor(Color.TRANSPARENT);
      // Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住。
      getWindow().getDecorView()
          .setSystemUiVisibility(getWindow().getDecorView().getSystemUiVisibility()
              | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
              | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      // Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住。
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
  }

这样如果需要改变状态栏颜色只需要使用代码

findViewById(R.id.status_bar).setBackgroundColor(getResources().getColor(R.color.primary_dark));

关键代码如下

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    >
  <!--内容区域-->
  <FrameLayout
      android:id="@+id/content"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />
  <!-- 自定义的状态栏和标题栏-->
  <!--自定义的顶部状态栏,和标题栏-->
  <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/top_bar"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:showIn="@layout/activity_common"
      >
    <!--自定义的状态栏,用来替代原有状态颜色-->
    <View
        android:id="@+id/status_bar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="?attr/statusBarColor"
        />

    <LinearLayout
        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/tool_bar_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:showIn="@layout/topbar_layout"
        >
      <!--标题栏-->
      <android.support.v7.widget.Toolbar
          android:id="@+id/tool_bar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
          />
      <!--阴影-->  
      <View
          android:id="@+id/tool_bar_shadow"
          android:layout_width="match_parent"
          android:layout_height="4dp"
          android:background="#d6d6d6"
          />

    </LinearLayout>
  </LinearLayout>
</FrameLayout>

// 设置状态栏透明(全屏)
setTransparentForWindow();
// 设置内容区域padding属性
mContentContainer.setPadding(0,
mSystemBarConfig.getStatusBarHeight() + mSystemBarConfig.getToolBarHeight(), 0, 0);
// 设置状态栏高度
ViewGroup.LayoutParams params = mStatusBar.getLayoutParams();
params.height = mSystemBarConfig.getStatusBarHeight();
mStatusBar.requestLayout();

2. CoordinatorLayout AppBarLayout (Toolbar ViewPager)嵌套使用(CoordinatorLayoutFragment.java)

这种布局的嵌套使用和普通布局有些不一样,当然基本的思路还是一样的. 首先设置状态栏透明以及全屏显示.然后通过对不同版本的显示效果差异进行调整.
因此我们首先也是通过 mActivity.setTransparentForWindow();设置全屏,状态栏透明.然后设置根布局的fitsystemWindow="true"使内容区域不被状态栏挡住.

  • 4.4系统会在状态栏部分空出来一部分显示的是根布局(CoordinatorLayout)的背景色.因此可以通过设置根布局(CoordinatorLayout)背景色来改变状态栏颜色.

  • 7.0系统我们可以直接设置状态栏颜色getWindow().setStatusBarColor(Color.xxx)

相关关键代码如下:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >

  <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
      >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="@string/app_name"
        />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

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

  <android.support.v4.view.ViewPager
      android:id="@+id/viewpager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="?activityBackgroundColor"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      />

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

</android.support.design.widget.CoordinatorLayout>
setTransparentForWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 设置状态栏颜色
mActivity.getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark));
    } 
else if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT) {
  // 通过设置背景色来修改状态栏颜色
mCoordinatorLayout.setBackgroundColor(getResources().getColor(R.color.primary_dark));
}
4.4效果 7.0效果
4.4效果-w340
4.4效果-w340
device-2017-06-09-110349-w340

3. DrawerLayout CoordinatorLayout AppBarLayout (Toolbar ViewPager)嵌套使用(DrawerLayoutFragment.java)

接上面的布局,如果在上面布局外面再嵌套一层DrawerLayout(布局层次结构如下图),这是我们需要的效果是抽屉展开时,抽屉上层的状态栏是透明的,而CoordinatorLayout区域是有颜色的状态栏.

  • 4.4系统无需做任何更改.通过mCoordinatorLayout.setBackgroundColor设置状态栏颜色
  • 7.0系统CoordinatorLayout布局的内容会被状态栏挡住,设置fitsystemWindow = "true" ,还是会被挡住,因此需要设置这个布局内容的paddingtop="状态栏高度"属性使内容不被挡住.可以通过mDrawerLayout.setStatusBarBackgroundColor设置状态栏颜色
  • 值得注意的是DrawerLayout的fitsystemWindow在布局文件里面必须设置为true,但是在代码里又要设置回false,否则7.0会有半透明的状态栏覆盖在DrawerLayout之上
  • 假设我们需要设置侧滑抽屉拉出后的状态栏颜色,这时候又需要区分7.0,4.4系统,对于Api21+直接设置状态栏颜色getWindow().setStatusBarColor,4.4系统则可以通过在activity.findViewById(android.R.id.content))的到的ViewGroup中添加一个高度为状态栏高度的View然后设置状态栏颜色.


    92D50605-80E7-44CE-89D4-1FABDD134D8F

实现沉浸式状态栏效果如下

4.4效果 7.0效果
Android4.4效果-w340
Android7.0效果-w340
抽屉展开后的效果 抽屉展开后的效果
Android4.4效果-w340
Android4.4效果-w340
Android7.0效果-w340
Android7.0效果-w340

关键代码如下:

setTransparentForWindow();
// 注意区分版本进行处理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      mCoordinatorLayout.setPadding(0, this.getSystemBarConfig().getStatusBarHeight(), 0, 0);
// 设置(覆盖在 CoordinatorLayout 上)状态栏颜色
mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primary_dark));
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      mCoordinatorLayout.setPadding(0, 0, 0, 0);
      // 状态栏颜色设置
mCoordinatorLayout.setBackgroundColor(getResources().getColor(R.color.primary_dark));
    }
    // 这一句是关键,布局文件里面设置这个属性为true,代码里面需要设置这个属性为false
    mDrawerLayout.setFitsSystemWindows(false);

CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout,嵌套使用(CollapsingToolbarLayoutFragment.java)

4.4效果 7.0效果
image.png
image.png
  • 对于7.0以上系统不需要特殊设置即可实现沉浸式状态栏
  • 4.4以下系统只需要将布局控件的fitsystemwindow设置为false即可
    关键代码如下:
mActivity.setTransparentForWindow();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      mMainContent.setFitsSystemWindows(false);
      mAppbar.setFitsSystemWindows(false);
      mCollapsingToolbar.setFitsSystemWindows(false);
      mImageView.setFitsSystemWindows(false);
      mToolbar.setFitsSystemWindows(false);
      ViewGroup.MarginLayoutParams layoutParams =
          (ViewGroup.MarginLayoutParams) mToolbar.getLayoutParams();
      layoutParams.setMargins(layoutParams.leftMargin,
          layoutParams.topMargin + mActivity.getSystemBarConfig().getStatusBarHeight(),
          layoutParams.rightMargin, layoutParams.bottomMargin);
    }
    ```


##最后按照惯例给出GitHub地址
[沉浸式状态栏实现](https://github.com/toutoumu/ControlDemo)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容