本课程介绍了如何注册侦听器,以便您的应用程序可以收到系统 UI 可见性变化的通知。如果您希望将 UI 的其他部分与系统栏的隐藏/显示同步,这将很有用。
注册侦听器
要接收系统 UI 可见性变化的通知,请将 View.OnSystemUiVisibilityChangeListener
注册到您的视图。这通常是您用来控制导航可见性的视图。
例如,您可以将以下代码添加到您的活动的 onCreate()
方法中
Kotlin
window.decorView.setOnSystemUiVisibilityChangeListener { visibility -> // Note that system bars will only be "visible" if none of the // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set. if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) { // TODO: The system bars are visible. Make any desired // adjustments to your UI, such as showing the action bar or // other navigational controls. } else { // TODO: The system bars are NOT visible. Make any desired // adjustments to your UI, such as hiding the action bar or // other navigational controls. } }
Java
View decorView = getWindow().getDecorView(); decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { // Note that system bars will only be "visible" if none of the // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set. if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { // TODO: The system bars are visible. Make any desired // adjustments to your UI, such as showing the action bar or // other navigational controls. } else { // TODO: The system bars are NOT visible. Make any desired // adjustments to your UI, such as hiding the action bar or // other navigational controls. } } });
通常,最好让 UI 与系统栏可见性变化保持同步。例如,您可以使用此侦听器在状态栏隐藏和显示时隐藏和显示操作栏。