Responder a alterações de visibilidade da IU
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Esta lição descreve como registrar um listener para que seu app possa ser notificado.
de mudanças na visibilidade da IU do sistema. Isso é útil se você deseja
sincronizar outras partes da interface de usuário com a ocultação/exibição de barras de sistema.
Registrar um listener
Para receber notificações sobre alterações na visibilidade da interface de usuário do sistema, registre um
View.OnSystemUiVisibilityChangeListener
à sua visualização.
Geralmente, essa é a visualização que você está usando para controlar a visibilidade da navegação.
Por exemplo, é possível adicionar esse código ao
Método 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.
}
}
});
Geralmente, é recomendável manter a IU sincronizada com as alterações na barra de sistema.
visibilidade. Por exemplo, você pode usar esse listener para ocultar e mostrar a barra de ações em
show com a barra de status oculta e mostrando.
O conteúdo e os exemplos de código nesta página estão sujeitos às licenças descritas na Licença de conteúdo. Java e OpenJDK são marcas registradas da Oracle e/ou suas afiliadas.
Última atualização 2025-07-27 UTC.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Não contém as informações de que eu preciso","missingTheInformationINeed","thumb-down"],["Muito complicado / etapas demais","tooComplicatedTooManySteps","thumb-down"],["Desatualizado","outOfDate","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Problema com as amostras / o código","samplesCodeIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-07-27 UTC."],[],[],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."]]