Questa lezione descrive come registrare un listener in modo che la tua app possa ricevere notifiche sulle modifiche alla visibilità dell'interfaccia utente di sistema. Questo è utile se vuoi sincronizzare altre parti della tua UI con le barre di sistema nascoste o mostrate.
Registra un listener
Per ricevere notifiche sulle modifiche alla visibilità dell'interfaccia utente di sistema, registra un View.OnSystemUiVisibilityChangeListener
nella tua vista.
In genere questa è la visualizzazione che utilizzi per controllare la visibilità della navigazione.
Ad esempio, potresti aggiungere questo codice al metodo onCreate()
dell'attività:
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. } } });
In genere è buona norma mantenere la UI sincronizzata con le modifiche di visibilità della barra di sistema. Ad esempio, puoi utilizzare questo listener per nascondere e mostrare la barra delle azioni mentre la barra di stato è nascosta e mostrata.