android 沉浸式状态栏


今日为了完成项目的需求,没办法需要加入沉浸式状态栏实现效果,android4.4加入沉浸式状态栏这一新内容,近日向大家推荐一个比较牛逼的库SystemBarTint

android 沉浸式状态栏_第1张图片

方法:

//是否设置状态栏透明

setTranslucentStatus(true);

//是否设置状态栏颜色

tintManager.setStatusBarTintEnabled(true);

//是否设置导航栏颜色

tintManager.setNavigationBarTintEnabled(true);

//设置状态栏颜色

tintManager.setStatusBarTintResource(R.color.red);// 通知栏所需颜色

要想使用沉浸式状态栏必须sdk>=19,所以在使用的时候必须添加判断

if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {

setTranslucentStatus(true);

tintManager=new SystemBarTintManager(this);

tintManager.setStatusBarTintEnabled(true);

tintManager.setStatusBarTintResource(R.color.red);// 通知栏所需颜色

}

SystemBarTint的原理就是首先将状态栏设置为透明,然后如图

android 沉浸式状态栏_第2张图片
沉浸式状态栏实现原理

实例化一个View,将View的宽度设为屏幕的宽度,高度设为状态栏的高度,然后将位置设置在屏幕顶部,然后为其设置颜色,颜色的色值就是我们通过setStatusBarTintResource()方法传入的颜色。

使用SystemBarTint 横竖屏转化的时候会出现问题,横屏状态下会多了一块状态栏,如果要去除此状态栏,切记tintManager=new SystemBarTintManager(this);

必须只实例化一次,实例化多次会即使你设置了颜色也不会起作用。

如果要隐藏掉状态栏只需要设置为透明即可:

tintManager.setStatusBarTintResource(R.color.transparent);// 通知栏所需颜色

补充要想实现沉浸式状态栏想过必须在布局中添加 android:fitsSystemWindows="true",这句话作用就是让顶部空出一个状态栏大小的位置。如果不添加这句话会变成如图式

android 沉浸式状态栏_第3张图片

未在布局中添加fitsSystemWindows的副作用


最后提示切记要在父布局中添加 android:fitsSystemWindows="true"

你可能感兴趣的:(android 沉浸式状态栏)