2010-03-02 25 views

cevap

119
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

Bir etkinlik çağrıldığında, onu yatay duruma getirecektir. ActivityInfo sınıfındaki diğer bayrakları arayın. Dikey olarak tekrar kilitleyebilir veya sensör/sürgü tahrikli hale getirebilirsiniz. Burada

diğer bilgiler: http://www.devx.com/wireless/Article/40792

+12

Teşekkürler. Bu harika çalışıyor. Geçerli yönlendirmeyi alacaktır. getResources(). GetConfiguration().yönlenme – Jared

+6

Dikkat! GetConfiguration() öğesinin ne döndürdüğünü ve setRequestedOrientation öğesinin ne istediği arasında ayrım yapmalısınız. Ayrıntılar için aşağıdaki cevabı bakın: –

+0

Bu yaklaşımla ilgili bir sorun var. [Bu yanıtı] (http://stackoverflow.com/a/3614089/1708390) –

27

Ben benzer bir durum var vardı gibiydi. Herhangi bir yönlendirmeyi desteklemek istedim, ancak iş akışında belirli bir noktadan sonra mevcut yönelimde kalmam gerekiyordu. korumalı iş akışının girişte

: My çözüm oldu korumalı iş akışının çıkışında

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 

:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
+1

Bu, en azından Android> = 16'da OQ'yu ele almaz. SetRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_NOSENSOR), cihazı portre olsa bile manzaraya ayarlayabilirken, soru, verilen kilitleme yönelimini ifade eder. – greg7gkb

+1

bana göre, nosensor ayarı yerine yatay moddayken portre moduna geri dönüyor ve bunun yerine SCREEN_ORIENTATION_LOCKED kullanmıştım ve benim için çalışmıştı – Jimmar

+0

@JiMMaR SCREEN_ORIENTATION_LOCKED Android için en iyi yoldur> = 18 Ancak daha düşük bir hedefi hedefliyorsanız , Bu çalışmıyor. Jp36'nın cevabını aşağıda kullanmanızı öneriyorum. –

95

döner getConfiguration neyi arasındaki farkın dikkatli olun ve hangi setRequestedOrientation istiyor - ikisi de int, ama farklı sabit tanımlardan geliyorlar.

Burada 180 derecede sağlayan bu ters portre cihazlarda çalışır

int currentOrientation = getResources().getConfiguration().orientation; 
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) { 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
} 
else { 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); 
} 
+11

SCREEN_ORIENTATION_USER_LANDSCAPE'yi kullanmayı tercih edebilirsiniz, çünkü bu ayarlarda kullanıcı ekran rotasyonunu devre dışı bırakmışsa 180 derece döndürme yapmasına izin vermez. Benzer şekilde, serbest rotasyona geri dönülürken, SCREEN_ORIENTATION_USER, SCREEN_ORIENTATION_SENSOR değerinden daha iyidir; –

+0

Parlak! Tersine döndürdüğünüzde, yeniden yapılandırma yapılmayacaksa, bunu eklemeniz gerekir. En azından test ettiğim cihazlarda. Bazı iletişim şovları vb. Sırasında yeniden yapılandırmayı durdurmak isteyip istemediğinizi bilmek gerçekten önemlidir. – sberezin

45

çevirir gelmesine karşın, mevcut yönünü kilitlemek ve peyzaj ters nasıl.

Kilit yönelim:

int orientation = getActivity().getRequestedOrientation(); 
    int rotation = ((WindowManager) getActivity().getSystemService(
      Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); 
    switch (rotation) { 
    case Surface.ROTATION_0: 
     orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 
     break; 
    case Surface.ROTATION_90: 
     orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 
     break; 
    case Surface.ROTATION_180: 
     orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; 
     break; 
    default: 
     orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; 
     break; 
    } 

    getActivity().setRequestedOrientation(orientation); 

kilidini yönelim:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
+4

Dönüş döndürme "" Ekranın "doğal" yönelimini döndürür. "[Kaynak] (http://developer.android.com/ referans/robot/görünüşüdür/Display.html # getRotation()). ROTATION_0'ın portre olduğunu söyleyen bir telefon muhtemelen doğru, ancak bir tablette "doğal" yönelimi büyük olasılıkla yataydır ve ROTATION_0, portre yerine manzaraya dönmelidir. – jp36

+0

@ jp36, Telefonla aynı doğal yönelime sahip Nexus 7'de test ettim. Daha büyük bir tablet üzerinde test ettiğiniz için teşekkür ederim. – pstoppani

+1

jp36 dediği gibi, doğal peyzaj yönelimli tabletlerde çalışmaz! – DominicM

19

Alternatif, @pstoppani cevap olarak (tablet desteği ile bu iradeyi cevabı @pstoppani için sadece cihazlar üzerinde çalış> 2.2)
üzerinde test edilmiştirve İşte

public static void lockOrientation(Activity activity) { 
    Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int rotation = display.getRotation(); 
    int tempOrientation = activity.getResources().getConfiguration().orientation; 
    int orientation = 0; 
    switch(tempOrientation) 
    { 
    case Configuration.ORIENTATION_LANDSCAPE: 
     if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) 
      orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 
     else 
      orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; 
     break; 
    case Configuration.ORIENTATION_PORTRAIT: 
     if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) 
      orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 
     else 
      orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; 
    } 
    activity.setRequestedOrientation(orientation); 
} 
3

Samsung Galaxy Tab 10.1 benim kodudur, bu yöntemlerin ekran biriyle kilitlemek ve bir kez görevi bitmiş olabilir unlockOrientation ile kilidini:

İşte
/** Static methods related to device orientation. */ 
public class OrientationUtils { 
    private OrientationUtils() {} 

    /** Locks the device window in landscape mode. */ 
    public static void lockOrientationLandscape(Activity activity) { 
     activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    } 

    /** Locks the device window in portrait mode. */ 
    public static void lockOrientationPortrait(Activity activity) { 
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    } 

    /** Locks the device window in actual screen mode. */ 
    public static void lockOrientation(Activity activity) { 
     final int orientation = activity.getResources().getConfiguration().orientation; 
     final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); 

     // Copied from Android docs, since we don't have these values in Froyo 2.2 
     int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8; 
     int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9; 

     // Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO 
     if (!BuildVersionUtils.hasGingerbread()) { 
      SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 
      SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 
     } 

     if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90){ 
      if (orientation == Configuration.ORIENTATION_PORTRAIT){ 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      } 
      else if (orientation == Configuration.ORIENTATION_LANDSCAPE){ 
       activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      } 
     } 
     else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) 
     { 
      if (orientation == Configuration.ORIENTATION_PORTRAIT){ 
       activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
      } 
      else if (orientation == Configuration.ORIENTATION_LANDSCAPE){ 
       activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
      } 
     } 
    } 

    /** Unlocks the device window in user defined screen mode. */ 
    public static void unlockOrientation(Activity activity) { 
     activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); 
    } 

} 
0

@pstoppani ait Xamarin dönüşüm Yukarıdaki cevap.

NOT: Bu bir Parça için Etkinliğini değiştirin. Bu, ile. Bir etkinlik içinde kullanılıyorsa.

public void LockRotation() 
{ 
    ScreenOrientation orientation; 

    var surfaceOrientation = Activity.WindowManager.DefaultDisplay.Rotation; 

    switch (surfaceOrientation) { 
     case SurfaceOrientation.Rotation0: 
      orientation = ScreenOrientation.Portrait; 
      break; 
     case SurfaceOrientation.Rotation90: 
      orientation = ScreenOrientation.Landscape; 
      break; 
     case SurfaceOrientation.Rotation180: 
      orientation = ScreenOrientation.ReversePortrait; 
      break; 
     default: 
      orientation = ScreenOrientation.ReverseLandscape; 
      break; 
    } 

    Activity.RequestedOrientation = orientation; 
} 

public void UnlockRotation() 
{ 
    Activity.RequestedOrientation = ScreenOrientation.Unspecified; 
} 

Bu daha önce kullanmış farklı bir yaklaşımla gitti, ama kullanım olabilecek test edilmedi.

+0

Bu, pstoppani'ninkiyle aynı yanıttır ve bir tablette başarısız olur. –

İlgili konular