Android状态栏&导航栏使用总结(重要)

参考

Android官方文档 # 隐藏状态栏

Android官方文档 # 隐藏导航栏

android view文档

Android状态栏&导航栏使用总结

控制点状态栏导航栏备注
完全隐藏View # setSystemUiVisibility
SYSTEM_UI_FLAG_FULLSCREEN
View # setSystemUiVisibility
SYSTEM_UI_FLAG_HIDE_NAVIGATION
布局延伸View # setSystemUiVisibility
SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
View # setSystemUiVisibility
SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
为避免页面内容被遮挡,我们可以考虑向 XML 布局文件添加 android:fitsSystemWindows 属性并设置为 true,系统将自动向布局的父布局添加对应bar的高度的padding。
背景颜色Window # setStatusBarColorWindow # setNavigationBarColor目前Android只支持设置为黑色或者白色
图标颜色View # setSystemUiVisibility
SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
View # setSystemUiVisibility
SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
(亮色模式:图标为黑色)
清除标记即可进入暗色模式
透明getWindow().addFlags
FLAG_TRANSLUCENT_STATUS
getWindow().addFlags
FLAG_TRANSLUCENT_NAVIGATION
会发现状态栏和toolbar重叠到了一起,在rootView中添加fitSystemwindows属性即可

Notes

1.通过View # setSystemUiVisibility改变状态栏或者导航栏状态后,当我们离开对应Activity时,这些改变会丢失,
因此当我们重新回到此Activity时,我们应该在onResume 或者 onWindowFocusChanged中重新设置需要的状态。

让内容显示在导航栏后面

Android 4.1 及更高版本中,您可以将应用的内容设置为显示在导航栏的后面,这样内容就不会随着导航栏的隐藏和显示调整大小。
可使用 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 执行此操作。 您可能还需要使用 SYSTEM_UI_FLAG_LAYOUT_STABLE 来帮助您的应用保持稳定布局。

让内容显示在状态栏后面

Android 4.1 及更高版本中,您可以将应用的内容设置为显示在状态栏的后面,这样内容大小就不会随着状态栏的隐藏和显示发生调整。
可使用 SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 执行此操作。 您可能还需要使用 SYSTEM_UI_FLAG_LAYOUT_STABLE 来帮助您的应用保持稳定布局。

当您使用此方法时,您要确保应用界面中的关键部分(例如,地图应用中的内置控件)不会被系统栏覆盖,否则会导致您的应用无法使用。
在大多数情况下,要处理这个问题,您可以通过向 XML 布局文件添加 android:fitsSystemWindows 属性并设置为 true
这会调整父级 ViewGroup 的内边距,为系统窗口留出空间。这对于大多数应用来说已经足够。

不过,在某些情况下,您可能需要修改默认的内边距以获得应用所需的布局。
要直接操控内容相对于系统栏的布局位置(占据的空间称为窗口的“内容嵌入”),请替换 fitSystemWindows(Rect insets)
当窗口的内容嵌入发生更改时,视图层次结构会调用 fitSystemWindows() 方法,以允许窗口相应地调整其内容。通过替换此方法,您可以根据需要处理嵌入内容(以及应用布局)。

flag

SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

Added in API level 16
Deprecated in API level 30

public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
This constant was deprecated in API level 30.
For floating windows, use LayoutParams#setFitInsetsTypes(int) with Type#statusBars() ()}. For non-floating windows that fill the screen, call Window#setDecorFitsSystemWindows(boolean) with false.

Flag for setSystemUiVisibility(int): View would like its window to be laid out as if it has requested SYSTEM_UI_FLAG_FULLSCREEN, even if it currently hasn’t. This allows it to avoid artifacts when switching in and out of that mode, at the expense that some of its user interface may be covered by screen decorations when they are shown. You can perform layout of your inner UI elements to account for non-fullscreen system UI through the fitSystemWindows(android.graphics.Rect) method.

Note: on displays that have a DisplayCutout, the window may still be placed differently than if SYSTEM_UI_FLAG_FULLSCREEN was set, if the window’s layoutInDisplayCutoutMode is LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT. To avoid this, use either of the other modes.

See also:

WindowManager.LayoutParams.layoutInDisplayCutoutMode
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER
Constant Value: 1024 (0x00000400)

SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

Added in API level 16
Deprecated in API level 30

public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
This constant was deprecated in API level 30.
For floating windows, use LayoutParams#setFitInsetsTypes(int) with Type#navigationBars(). For non-floating windows that fill the screen, call Window#setDecorFitsSystemWindows(boolean) with false.

Flag for setSystemUiVisibility(int): View would like its window to be laid out as if it has requested SYSTEM_UI_FLAG_HIDE_NAVIGATION, even if it currently hasn’t. This allows it to avoid artifacts when switching in and out of that mode, at the expense that some of its user interface may be covered by screen decorations when they are shown. You can perform layout of your inner UI elements to account for the navigation system UI through the fitSystemWindows(android.graphics.Rect) method.

Constant Value: 512 (0x00000200)

SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

Added in API level 23
Deprecated in API level 30

public static final int SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
This constant was deprecated in API level 30.
Use WindowInsetsController#APPEARANCE_LIGHT_STATUS_BARS instead.

Flag for setSystemUiVisibility(int): Requests the status bar to draw in a mode that is compatible with light status bar backgrounds.

For this to take effect, the window must request FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS but not FLAG_TRANSLUCENT_STATUS.

See also:

R.attr.windowLightStatusBar
Constant Value: 8192 (0x00002000)

SYSTEM_UI_FLAG_LAYOUT_STABLE

Added in API level 16
Deprecated in API level 30

public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE
This constant was deprecated in API level 30.
Use WindowInsets#getInsetsIgnoringVisibility(int) instead to retrieve insets that don’t change when system bars change visibility state.

Flag for setSystemUiVisibility(int): When using other layout flags, we would like a stable view of the content insets given to fitSystemWindows(android.graphics.Rect). This means that the insets seen there will always represent the worst case that the application can expect as a continuous state. In the stock Android UI this is the space for the system bar, nav bar, and status bar, but not more transient elements such as an input method. The stable layout your UI sees is based on the system UI modes you can switch to. That is, if you specify SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN then you will get a stable layout for changes of the SYSTEM_UI_FLAG_FULLSCREEN mode; if you specify SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN and SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION, then you can transition to SYSTEM_UI_FLAG_FULLSCREEN and SYSTEM_UI_FLAG_HIDE_NAVIGATION with a stable layout. (Note that you should avoid using SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION by itself.) If you have set the window flag WindowManager.LayoutParams#FLAG_FULLSCREEN to hide the status bar (instead of using SYSTEM_UI_FLAG_FULLSCREEN), then a hidden status bar will be considered a “stable” state for purposes here. This allows your UI to continually hide the status bar, while still using the system UI flags to hide the action bar while still retaining a stable layout. Note that changing the window fullscreen flag will never provide a stable layout for a clean transition.

If you are using ActionBar in overlay mode with Window.FEATURE_ACTION_BAR_OVERLAY, this flag will also impact the insets it adds to those given to the application.

Constant Value: 256 (0x00000100)

SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

Added in API level 16
Deprecated in API level 30

public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
This constant was deprecated in API level 30.
For floating windows, use LayoutParams#setFitInsetsTypes(int) with Type#statusBars() ()}. For non-floating windows that fill the screen, call Window#setDecorFitsSystemWindows(boolean) with false.

Flag for setSystemUiVisibility(int): View would like its window to be laid out as if it has requested SYSTEM_UI_FLAG_FULLSCREEN, even if it currently hasn’t. This allows it to avoid artifacts when switching in and out of that mode, at the expense that some of its user interface may be covered by screen decorations when they are shown. You can perform layout of your inner UI elements to account for non-fullscreen system UI through the fitSystemWindows(android.graphics.Rect) method.

Note: on displays that have a DisplayCutout, the window may still be placed differently than if SYSTEM_UI_FLAG_FULLSCREEN was set, if the window’s layoutInDisplayCutoutMode is LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT. To avoid this, use either of the other modes.

See also:

WindowManager.LayoutParams.layoutInDisplayCutoutMode
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER
Constant Value: 1024 (0x00000400)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值