Interface upside-down on temporary lock orientation
I'm having a problem with Android 2.2 and screen orientation. I have checkbox on my interface that when checked, the orientation must be locked on the current orientation, so I did the following code:
Activity a = (Activity) getContext();
if (isChecked) {
if (getResources().getConfiguration().orientation == configuration.ORIENTATION_LANDSCAPE)
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
The problem with this is that if I turn upside down the device, my scr开发者_开发技巧een will rotate to that, and when I click on my "lock orientation", getResources().getConfiguration().orientation will return SCREEN_ORIENTATION_PORTRAIT and my code will lock the orientation to SCREEN_ORIENTATION_PORTRAIT and the interface will be upside-down.
I can see that on Gingerbread (http://developer.android.com/reference/ android/R.attr.html#screenOrientation), there is a reversePortait and reverseLandscape to solve this, but I really need this code to run on 2.2, so is there anyway to set the screen to upside down? Or is there any other way to lock the rotation?
Use ScreenRotation. ScreenOrientation is deprecated and does not know about upside-down portrait orientation.
精彩评论