响应界面可见度变更
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本课介绍了如何注册监听器,以便您的应用接收通知
系统界面可见性变更如果您想
使界面的其他部分与系统栏的隐藏/显示同步。
注册监听器
要获取有关系统界面可见性更改的通知,请注册
View.OnSystemUiVisibilityChangeListener
添加到您的视图中。
这通常是您用来控制导航可见性的视图。
例如,您可以将此代码添加到 activity 的
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.
}
}
});
一般而言,最好使界面与系统栏中的更改保持同步
可见性。例如,您可以使用此监听器在
同时隐藏和显示状态栏。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Respond to UI visibility changes\n\nThis lesson describes how to register a listener so that your app can get notified\nof system UI visibility changes. This is useful if you want to\nsynchronize other parts of your UI with the hiding/showing of system bars.\n\nRegister a Listener\n-------------------\n\nTo get notified of system UI visibility changes, register an\n[View.OnSystemUiVisibilityChangeListener](/reference/android/view/View.OnSystemUiVisibilityChangeListener) to your view.\nThis is typically the view you are using to control the navigation visibility.\n\nFor example, you could add this code to your activity's\n[onCreate()](/reference/android/app/Activity#onCreate(android.os.Bundle)) method: \n\n### Kotlin\n\n```kotlin\nwindow.decorView.setOnSystemUiVisibilityChangeListener { visibility -\u003e\n // Note that system bars will only be \"visible\" if none of the\n // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.\n if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {\n // TODO: The system bars are visible. Make any desired\n // adjustments to your UI, such as showing the action bar or\n // other navigational controls.\n } else {\n // TODO: The system bars are NOT visible. Make any desired\n // adjustments to your UI, such as hiding the action bar or\n // other navigational controls.\n }\n}\n```\n\n### Java\n\n```java\nView decorView = getWindow().getDecorView();\ndecorView.setOnSystemUiVisibilityChangeListener\n (new View.OnSystemUiVisibilityChangeListener() {\n @Override\n public void onSystemUiVisibilityChange(int visibility) {\n // Note that system bars will only be \"visible\" if none of the\n // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.\n if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {\n // TODO: The system bars are visible. Make any desired\n // adjustments to your UI, such as showing the action bar or\n // other navigational controls.\n } else {\n // TODO: The system bars are NOT visible. Make any desired\n // adjustments to your UI, such as hiding the action bar or\n // other navigational controls.\n }\n }\n});\n```\n\nIt's generally good practice to keep your UI in sync with changes in system bar\nvisibility. For example, you could use this listener to hide and show the action bar in\nconcert with the status bar hiding and showing."]]