2011-11-25 29 views
21

Estoy intentando crear un botón en el que pueda ocultar o mostrar la barra de estado en mi tableta.Android: mostrar/ocultar la barra de estado/barra de estado

He puesto en el onCreate

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 

y en los botones espectáculo:

WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; 
getWindow().setAttributes(attrs); 

ocultar:

WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
getWindow().setAttributes(attrs); 

ninguna pista/tips sobre el tema?

// editar

He mirado en esta receta aquí: http://android.serverbox.ch/?p=306 y cambiado de código como este:

private void hideStatusBar() throws IOException, InterruptedException { 
    Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","service call activity 79 s16 com.android.systemui"}); 
    proc.waitFor(); 
} 

private void showStatusBar() throws IOException, InterruptedException { 
    Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"}); 
    proc.waitFor(); 
} 

Así que si hago clic en los botones de mi se llama una los métodos que pueden ver que algo está sucediendo porque la aplicación está esperando algunos segundos. También miré en LockCat y veo que algo está sucediendo.

espectáculo: http://pastebin.com/CidTRSTi ocultar: http://pastebin.com/iPS6Kgbp

+0

Ejemplo http://stackoverflow.com/a/35886019/4395114 –

Respuesta

78

¿Tiene el tema de pantalla completa establecido en el manifiesto?

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 

no creo que usted será capaz de ir a pantalla completa sin este.

me gustaría utilizar el siguiente para agregar y quitar el indicador de pantalla completa:

// Hide status bar 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
// Show status bar 
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
+0

No, no he añadieron al manifiesto.Pero cuando intento, obtengo el error: Error: no se encontró ningún recurso que coincida con el nombre de pila (en 'theme' con el valor '@android: style/ Theme.Dark.NoTitleBar.Fullscreen'). " – B770

+1

@ B770: Hay no tiene nombre 'Theme.Dark.NoTitleBar.Fullscreen'. Hay uno llamado' Theme.NoTitleBar.Fullscreen'. Sin embargo, no puede deshacerse de la barra del sistema en los dispositivos Android 3.0+ que carecen de los botones HOME y BACK fuera de la pantalla. – CommonsWare

+0

@CommonWare: Encontré esto: http://forum.xda-developers.com/showthread.php?t=1265397 aquí es posible ocultar y mostrar la barra de estado. He instalado la ROM "Overcome" donde este funktion está incluido y funciona. Así que pensé que también sería posible poner este funktion en otro botón ... – B770

25

Para algunas personas, Mostrando barra de estado en la limpieza de FLAG_FULLSCREEN no funcione,

Aquí está la solución que funcionó para mí , (Documentation) (Flag Reference)

Ocultar barra de estado

// Hide Status Bar 
if (Build.VERSION.SDK_INT < 16) { 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
} 
else { 
    View decorView = getWindow().getDecorView(); 
    // Hide Status Bar. 
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; 
    decorView.setSystemUiVisibility(uiOptions); 
} 

Mostrar barra de estado

if (Build.VERSION.SDK_INT < 16) { 
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } 
    else { 
     View decorView = getWindow().getDecorView(); 
     // Show Status Bar. 
     int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE; 
     decorView.setSystemUiVisibility(uiOptions); 
    } 
-2

Referencia - https://developer.android.com/training/system-ui/immersive.html

// This snippet shows the system bars. It does this by removing all the flags 
// except for the ones that make the content appear under the system bars. 
private void showSystemUI() { 
    mDecorView.setSystemUiVisibility(
      View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
      | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
      | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
} 
2

Una de las características introducidas en KitKat es "modo envolvente". El modo inmersivo le brinda al usuario la capacidad de mostrar/ocultar la barra de estado y la barra de navegación con un deslizamiento. Para intentarlo, haz clic en el botón "Alternar modo inmersivo", ¡y luego prueba deslizando la barra hacia adentro y hacia afuera!

Ejemplo:

public void toggleHideyBar() { 

     int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility(); 
     int newUiOptions = uiOptions; 
     boolean isImmersiveModeEnabled = 
       ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); 
     if (isImmersiveModeEnabled) { 
      Log.i(TAG, "Turning immersive mode mode off. "); 
     } else { 
      Log.i(TAG, "Turning immersive mode mode on."); 
     } 

     // Navigation bar hiding: Backwards compatible to ICS. 
     if (Build.VERSION.SDK_INT >= 14) { 
      newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; 
     } 

     // Status bar hiding: Backwards compatible to Jellybean 
     if (Build.VERSION.SDK_INT >= 16) { 
      newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN; 
     } 

     if (Build.VERSION.SDK_INT >= 18) { 
      newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 
     } 

     getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions); 
     //END_INCLUDE (set_ui_flags) 
    } 
Cuestiones relacionadas