如果应用有特殊需求,需要手动将屏幕切换为横屏或竖屏,可以使用以下代码实现:
int newOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("nuorientation", current == newOrientation ?android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:newOrientation);
editor.commit();
setRequestedOrientation(current == newOrientation ?
android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:newOrientation);