WindowInsetsController(不推荐)

两种方法可以引进WindowInsetsController,一种直接经过APIval controller = window.decorView.windowInsetsController获取,留意该办法在API30及以上才有,常用办法如下:

设置体系栏色彩

void setSystemBarsAppearance(@Appearance int appearance, @Appearance int mask);
@IntDef(flag = true, value = {APPEARANCE_OPAQUE_STATUS_BARS, APPEARANCE_OPAQUE_NAVIGATION_BARS,APPEARANCE_LOW_PROFILE_BARS, APPEARANCE_LIGHT_STATUS_BARS,APPEARANCE_LIGHT_NAVIGATION_BARS, APPEARANCE_SEMI_TRANSPARENT_STATUS_BARS,APPEARANCE_SEMI_TRANSPARENT_NAVIGATION_BARS})
@interface Appearance {}

如:

setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS) //状态栏设置为黑色
setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS) //状态栏设置成白色

设置体系栏Behavior

void setSystemBarsBehavior(@Behavior int behavior);
int BEHAVIOR_DEFAULT = 1; //默认设置,经过滑动体系栏展现出来后不再躲藏
int BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE = 2; //体系栏展现一会后再次消失
@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {BEHAVIOR_DEFAULT, BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE})
@interface Behavior { }

展现与躲藏

void hide(@InsetsType int types); //躲藏体系栏(包含状态栏、导航栏、或许两者都包含)
void show(@InsetsType int types); //展现体系栏(包含状态栏、导航栏、或许两者都包含)
@InsetsType常用设置:
- WindowInsets.Type.statusBars() //状态栏
- WindowInsets.Type.navigationBars() //导航栏
- WindowInsets.Type.systemBars() //体系栏(包含状态栏、导航栏)
- WindowInsets.Type.ime() //键盘 (需求界面有输入控件且获取到焦点才干起作用)
- WindowInsets.Type.captionBar() //标题栏

留意:经过setSystemBarsBehavior()设置的有两个值,一个是BEHAVIOR_DEFAULT:默认设置,当体系栏经过hide()躲藏后,从躲藏栏的屏幕边际滑动,体系栏会再次显现且不再消失;另一个是BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE: 体系栏经过hide()躲藏后,从躲藏栏的屏幕边际滑动,体系栏会再次显现且会在一段时间后再次自动躲藏

ViewCompat.getWindowInsetsController(推荐)

除了上述经过体系API办法获取,还可以经过AndroidX的方法引进: implementation 'androidx.core:core-ktx:1.5.0',获取WindowInsetsController

val controller = ViewCompat.getWindowInsetsController(window.decorView)

设置体系栏色彩

//设置状态栏色彩(6.0及以上收效), true为黑色,反之为白色
public void setAppearanceLightStatusBars(boolean isLight) 
//设置导航栏色彩(8.0及以上收效) 
public void setAppearanceLightNavigationBars(boolean isLight)

设置体系栏Behavior

//状态栏行为
public void setSystemBarsBehavior(@Behavior int behavior)

@Behavior中的值如下:

@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {BEHAVIOR_SHOW_BARS_BY_TOUCH, BEHAVIOR_SHOW_BARS_BY_SWIPE,
            BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE})
@interface Behavior {}

@Behavior中重点关注下:WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE,体系栏经过hide()躲藏后,从躲藏栏的屏幕边际滑动,体系栏会再次显现且会在一段时间后再次自动躲藏。

展现与躲藏

//体系栏展现
public void show(@InsetsType int types)
//体系栏躲藏
public void hide(@InsetsType int types)
@InsetsType常用设置:
- WindowInsetsCompat.Type.statusBars() //状态栏
- WindowInsetsCompat.Type.navigationBars() //导航栏
- WindowInsetsCompat.Type.systemBars() //体系栏(包含状态栏、导航栏)
- WindowInsetsCompat.Type.ime() //键盘 (需求界面有输入控件且获取到焦点才干起作用)
- WindowInsetsCompat.Type.captionBar() //标题栏