2014-10-27 82 views
4

我使用:安卓:防止方向改變編程

setRequestedOrientation(getResources().getConfiguration().orientation); 

,稍後:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 

它可以防止取向改變,直到任務完成,但它僅適用於肖像,當應用程序在風景上時,它不會停止方向改變。

有什麼建議嗎?
OIM〜

+0

: orientation = potrait在清單中的每個活動中 – therealprashant 2014-10-27 05:03:22

+0

這可以幫助您以編程方式禁用旋轉設備:http://stackoverflow.com/a/3614089/3864698 – QArea 2014-10-27 11:07:18

+0

@therealprashant我不是試圖修復整個應用程序運行的方向,而只是在關鍵任務運行的時候運行。 – 2014-10-27 16:49:09

回答

5

我得到它的正確在所有情況下現在的工作。

要解決屏幕:

if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_0) 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_90) 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_270) 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 

,然後再允許轉:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 

OIM〜

如果你想保持固定potrait模式,然後使用Android方向
+0

你在平板電腦上測試它嗎?片劑有不同的方向。 Rotation_0表示風景。 – Oximer 2015-12-07 19:01:05

+0

對不起,對於遲來的答案,我已經在平板電腦上測試了一次應用程序,並且據我記得這很好,但我並沒有積極嘗試測試。抱歉 – 2016-01-28 16:03:56

0

在與您的活動清單文件試試這個

android:screenOrientation="sensorLandscape" 
    android:windowSoftInputMode="stateAlwaysHidden" 

內部應用程序標記清單文件試試這個

android:hardwareAccelerated="true" 
    android:largeHeap="true" 
+0

完全相同的行爲,它在縱向時工作正常,但在橫向上不做任何事情。 – 2014-10-27 04:50:31

+0

如果它幫助你..讓我知道,如果沒有,然後讓我知道.. – DJhon 2014-10-27 05:07:23

+0

謝謝你嘗試,但保持完全相同,它在肖像時正常工作,而在景觀沒有。我保留了前面的幾行並添加了第二行。 – 2014-10-27 06:28:13