Lock Screen Orientation in Android
Posted on 2010-06-21 23:46 David.Ko 閱讀(1612) 評(píng)論(0) 編輯 收藏 所屬分類: AndroidMartin Belcher | 26th May 2010 | ||||||||
Intro
This article describes how to force the orientation of an Android view not to change ie screen not to rotate. How to lock the orientationIn the onCreateDialog(int) event of the activity use the setRequestedOrientation(int) method to set the screen orientation to your chosen setting. The activity will stay in this orientation regardless of if the device is tilted or not.
How to detect the current orientationTo programmatically detect the current orientation of the activity use the following code snippet. Theorientation property of the Configuration class returns three possible values corresponding to Landscape, Portrait and Square.
Example : Locking rotation while performing an action.You might wish to disable the screen rotation whilst performing an action or by user command, to do this you need to combine the above samples to detect the current orientation and lock the display to that orientation.
Once your action has completed you may wish to enable screen rotation again, see the next section for an example on how to do this. How to re-enable screen rotationTo enable the orientation to be automatically changed on device tilt simply pass thesetRequestedOrientation(int) method the enumeration value for an unspecified orientation.
|