0

在我的活動中,我有以下代碼。Android - 即使在自動旋轉開啓時將應用程序屏幕方向設置爲風景,設備屏幕也會翻轉

[Activity(Label = "SampleActivity", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.KeyboardHidden)] 

在OnCreate中() - >

if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Gingerbread) { 
       RequestedOrientation = ScreenOrientation.SensorLandscape; 
      } 
      else { 
       RequestedOrientation = ScreenOrientation.Landscape; 
      } 

還在屏幕在橫向模式本身翻轉如果自動旋轉爲ON試用版清單想知道爲什麼發生這種情況。是嗎?任何避免翻轉的選項?

需要幫助。提前感謝。

回答

1

爲了在縱向模式下鎖定您的應用程序,您必須在清單文件中添加以下行,並在清單文件中定義您的活動。

<activity android:name=".yourActivity" android:screenOrientation="portrait"/> 

如果要鎖定橫向模式,請將橫向替換爲縱向。

+0

上面的代碼是什麼我已經添加在Xamarin中。所以,我已經加入了Activity。謝謝。 – sha

+0

我歡迎@sha,你可以接受我的答案,如果你喜歡:) – Awadesh

0

嘗試Menifest文件中寫入這樣的:

<activity 
      android:name="SampleActivity" 
      android:screenOrientation="landscape" // or "portrait" if you want 
      android:configChanges="keyboardHidden|screenLayout|screenSize|orientation" 
      android:label="@string/app_name" /> 

對於Xamarin根據您的需要,因爲更改代碼:(ScreenOrientation.Portrait/ScreenOrientation.Landscape)

[Activity(Label = "SampleActivity", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.KeyboardHidden),ScreenOrientation = ScreenOrientation.Portrait)] 
+0

我需要將它們添加到Xamarin中沒有顯示的Activity中。我會嘗試添加screenLayout並更新你的。謝謝。 – sha

+0

是的,您可能會在添加後解決您的問題 –

+0

您可以檢查更新的答案以獲取更多信息。 –