2013-08-16 15 views

risposta

133

Sì, è implementabile!

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

ActivityInfo

http://developer.android.com/reference/android/content/pm/ActivityInfo.html

rinviare la link:

Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait); 
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape); 

buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    } 

}); 

buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    } 

}); 

http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html

+0

Grazie. Ma questa funzione deve essere eseguita su un dispositivo rooted? Sembra non funzionare sul mio cellulare, ma sul mio tablet rooted. – Sam

+0

No, non è così .. In realtà ho testato il codice in quel collegamento prima di postare. Stava funzionando nel mio dispositivo .. – Hariharan

+0

ok, sì, hai ragione. Ho appena provato un altro dispositivo e funziona bene. bene, il mio Samsung Galaxy Nexus non funziona. Non so perché. – Sam

24

Sì, è possibile impostare l'orientamento dello schermo programatically ogni volta che si desidera utilizzare:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

rispettivamente per la modalità ritratto e paesaggio. Il metodo setRequestedOrientation() è disponibile per la classe Attività, quindi può essere utilizzato all'interno della tua attività.

e in questo modo si può ottenere l'orientamento dello schermo corrente e impostarlo in maniera adeguata a seconda del suo stato attuale:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
final int orientation = display.getOrientation(); 
// OR: orientation = getRequestedOrientation(); // inside an Activity 

// set the screen orientation on button click 
Button btn = (Button) findViewById(R.id.yourbutton); 
btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       switch(orientation) { 
        case Configuration.ORIENTATION_PORTRAIT: 
         setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
         break; 
        case Configuration.ORIENTATION_LANDSCAPE: 
         setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
         break;     
       } 
      } 
    }); 

Tratto da qui: http://techblogon.com/android-screen-orientation-change-rotation-example/

EDIT

Inoltre, puoi ottenere l'orientamento dello schermo utilizzando Configuration:

Activity.getResources().getConfiguration().orientation 
+2

Suggerisco di utilizzare 'getRequestedOrientation()' per ottenere l'orientamento dello schermo corrente: http://stackoverflow.com/a/21909327/1037294 –

+0

Risposta esaustiva. – pdegand59

14

Consente di impostare l'orientamento dello schermo:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

o

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

e non dimenticate di aggiungere questo al vostro manifesto:

android:configChanges = "orientation" 
+2

È necessario sia l'orientamento "" | screenSize "', Guarda qui: https://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange – Benny

+0

api 27, è bacato, non utilizzare manifest opzione – Mike

12

Ove possibile , per favore non usare SCREEN_ORIENTATION_LANDSCAPE o SCREEN_ORIENTATION_PORTRAIT. Invece usano:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 

Questi permettono di orientare il dispositivo sia l'orientamento orizzontale, o uno verticale, rispettivamente. Se hai mai dovuto giocare una partita con un cavo di ricarica che ti viene portato nello stomaco, allora sai esattamente perché avere entrambi gli orientamenti disponibili è importante per l'utente.

Nota: per i telefoni, almeno alcuni che ho controllato, consente solo la modalità verticale "sul lato destro", tuttavia SENSOR_PORTRAIT funziona correttamente sui tablet.

Nota: questa funzionalità è stata introdotta nel API Livello 9, quindi se è necessario supportare 8 o inferiore (non probabile a questo punto), poi invece utilizzare:

setRequestedOrientation(Build.VERSION.SDK_INT < 9 ? 
         ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : 
         ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
setRequestedOrientation(Build.VERSION.SDK_INT < 9 ? 
         ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : 
         ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 
Problemi correlati