2011-10-07 19 views
17

Ho bisogno di aiuto per attivare la modalità a schermo intero. Ho un'impostazione in una schermata delle preferenze per andare a schermo intero. In onResume di mia attività principale che ho:Attiva/disattiva Modalità schermo intero

if(mFullscreen == true) { 
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 

      } else 
      { 
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 
       getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 

      } 

Ma questo non sembra funzionare perché ha bisogno di essere chiamato prima setContentView giusto?

... Ma inoltre, ho requestWindowFeature(Window.FEATURE_NO_TITLE); prima del setContentView e toglie il titolo E la barra di stato ... Qualcuno può offrire aiuto?

--- Modifica --- Ok, ho riscontrato un errore che causava il malfunzionamento. Quindi lo fa davvero. Ora, ho solo bisogno di sapere come attivare la barra del titolo.

risposta

22
private void setFullscreen(boolean fullscreen) 
{ 
    WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
    if (fullscreen) 
    { 
     attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
    } 
    else 
    { 
     attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; 
    } 
    getWindow().setAttributes(attrs); 
} 
3

La mia soluzione combina le risposte da: risposta

ho aggiunto questi metodi per la mia attività. Per attivare lo schermo intero, utilizzare setFullScreen(!isFullScreen()).

public boolean isFullScreen() { 

    return (getWindow().getAttributes().flags & 
     WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0; 
} 

@SuppressLint("NewApi") 
public void setFullScreen(boolean full) { 

    if (full == isFullScreen()) { 
     return; 
    } 

    Window window = getWindow(); 
    if (full) { 
     window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } else { 
     window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } 

    if (Build.VERSION.SDK_INT >= 11) { 
     if (full) { 
      getActionBar().hide(); 
     } else { 
      getActionBar().show(); 
     } 
    } 
} 

Nel mio caso, volevo un pulsante di menu per eseguire la commutazione. Il problema: su un dispositivo senza un pulsante del menu hardware, nascondendo la barra delle azioni nasconde anche l'interruttore per tornare dallo schermo intero. Così, ho aggiunto qualche logica in più in modo che nasconda solo la barra delle azioni se il dispositivo ha un pulsante del menu hardware. Tieni presente che i dispositivi che eseguono l'SDK 11-13 non ne hanno uno.

if (Build.VERSION.SDK_INT >= 14 
     && ViewConfiguration.get(this).hasPermanentMenuKey()))) { 

     if (full) { 
      getActionBar().hide(); 
     } else { 
      getActionBar().show(); 
     } 
    } 

dispositivi più vecchi (in esecuzione di pan di zenzero o precedente) hanno una barra del titolo, invece di una barra delle operazioni. Il seguente codice lo nasconderà, ma tieni presente che la barra del titolo non può essere mostrata/nascosta una volta avviata l'attività. Ho incluso un messaggio per l'utente nel mio menu di aiuto affermando che le modifiche a schermo intero potrebbero non avere pieno effetto sui dispositivi più vecchi fino a quando non riavviano l'app/attività (che ovviamente presuppone che si mantenga la selezione ed esegua questo codice solo se lo desiderano a schermo intero).

// call before setContentView() 
    if (Build.VERSION.SDK_INT < 11) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
    } 
6

Da Jellybean (4.1) esiste un nuovo metodo che non si basa su WindowManager. Invece usa setSystemUiVisibility fuori dalla finestra, questo ti dà un controllo più granulare sulle barre di sistema rispetto all'uso dei flag WindowManager. Questo è il modo di attivare a schermo intero:

if (Build.VERSION.SDK_INT < 16) { //ye olde method 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
} else { // Jellybean and up, new hotness 
    View decorView = getWindow().getDecorView(); 
    // Hide the status bar. 
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; 
    decorView.setSystemUiVisibility(uiOptions); 
    // Remember that you should never show the action bar if the 
    // status bar is hidden, so hide that too if necessary. 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
} 

E questo è il modo per ripristinare il codice di cui sopra:

if (Build.VERSION.SDK_INT < 16) { //ye olde method 
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
} else { // Jellybean and up, new hotness 
    View decorView = getWindow().getDecorView(); 
    // Hide the status bar. 
    int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE; 
    decorView.setSystemUiVisibility(uiOptions); 
    // Remember that you should never show the action bar if the 
    // status bar is hidden, so hide that too if necessary. 
    ActionBar actionBar = getActionBar(); 
    actionBar.show(); 
} 
+0

Nota che potrebbe essere necessario richiamare 'getSupportActionBar()'. – JakeSteam

+0

Io uso questo metodo, sulla barra di stato della macchina huawei è nascosta ma la vista non può regolare la posizione nella parte superiore dello schermo – Carl

3

C'è una corta levetta piena attuazione metodo schermo:

private void toggleFullscreen() { 
    WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
    attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
    getWindow().setAttributes(attrs); 
} 

utilizza bit per bit Logica XOR per passare da FLAG_FULLSCREEN.

8
/** 
* toggles fullscreen mode 
* <br/> 
* REQUIRE: android:configChanges="orientation|screenSize" 
* <pre> 
* sample: 
*  private boolean fullscreen; 
*  ................ 
*  Activity activity = (Activity)context; 
*  toggleFullscreen(activity, !fullscreen); 
*  fullscreen = !fullscreen; 
* </pre> 
*/ 
private void toggleFullscreen(Activity activity, boolean fullscreen) { 
    if (Build.VERSION.SDK_INT >= 11) { 
     // The UI options currently enabled are represented by a bitfield. 
     // getSystemUiVisibility() gives us that bitfield. 
     int uiOptions = activity.getWindow().getDecorView().getSystemUiVisibility(); 
     int newUiOptions = uiOptions; 
     boolean isImmersiveModeEnabled = 
       ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); 
     if (isImmersiveModeEnabled) { 
      Log.i(context.getPackageName(), "Turning immersive mode mode off. "); 
     } else { 
      Log.i(context.getPackageName(), "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; 
     } 

     // Immersive mode: Backward compatible to KitKat. 
     // Note that this flag doesn't do anything by itself, it only augments the behavior 
     // of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample 
     // all three flags are being toggled together. 
     // Note that there are two immersive mode UI flags, one of which is referred to as "sticky". 
     // Sticky immersive mode differs in that it makes the navigation and status bars 
     // semi-transparent, and the UI flag does not get cleared when the user interacts with 
     // the screen. 
     if (Build.VERSION.SDK_INT >= 18) { 
      newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 
     } 
     activity.getWindow().getDecorView().setSystemUiVisibility(newUiOptions); 
    } else { 
     // for android pre 11 
     WindowManager.LayoutParams attrs = activity.getWindow().getAttributes(); 
     if (fullscreen) { 
      attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
     } else { 
      attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; 
     } 
     activity.getWindow().setAttributes(attrs); 
    } 

    try { 
     // hide actionbar 
     if (activity instanceof ActionBarActivity) { 
      if (fullscreen) ((ActionBarActivity) activity).getSupportActionBar().hide(); 
      else ((ActionBarActivity) activity).getSupportActionBar().show(); 
     } else if (Build.VERSION.SDK_INT >= 11) { 
      if (fullscreen) activity.getActionBar().hide(); 
      else activity.getActionBar().show(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // set landscape 
    // if(fullscreen) activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
    // else activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 
} 

il mio codice funziona bene con Android 2.3 e 4.4.2

+2

Ottimo metodo di aggiornamento! Due cose però: 1) le modifiche di newUiOptions mancano delle parti "full =" e = ~ xxx ", 2) L'ultima appcompat lib usa AppCompatActivity invece di ActionBarActivity. –

+0

un grande ringraziamento ha funzionato per me – Richi

Problemi correlati