设置当前 activity 的屏幕方向
在 AndroidManifest 中设置
android:screenOrientation="landscape"
unspecified - 默认值,由系统选择显示方向
landscape - 橫向
portrait - 纵向
reverseLandscape - 反横向(API >= 9)
reversePortrait - 反纵向(API >= 9)
user - 用户当前的首选方向
behind - 与Activity堆栈下的方向相同
sensor - 根据物理传感器方向3/4个方向(取决于设备)
fullSensor - 根据物理传感器方向4个方向
nosensor - 不按照物理传感器方向,除此之外与"unspecified"无区别
sensorLandscape - 按照物理传感器,只在横向(2个方向)进行翻转(API >= 9)
sensorPortrait - 按照物理传感器,只在纵向(2个方向)进行翻转(API >= 9)
userLandscape - 按照用户选择,锁定一个横向,或者按照物理传感器进行横向的翻转(API >= 18)
userPortrait - 按照用户选择,锁定一个纵向,或者按照物理传感器进行纵向的翻转(API >= 18)
fullUser - 如果用户锁定了屏幕,它与"user"作用一致,如果是解锁了旋转,它与"fullSensor"作用一致(API >= 18)
locked - 锁定了屏幕当前方向(API >= 18)
在代码中设置
//强制正向横屏,对应 AndroidManifest中的 screenOrientation="Landscape"
int landscape = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
//强制反向横屏,AndroidManifest中的 screenOrientation="reverseLandscape"
int reverseLandscape = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
//自由切换横屏,AndroidManifest中的 screenOrientation="sensorLandscape"
int sensorLandscape =ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
//设置强制正向横屏
setRequestedOrientation(landscape);
//当前activity设置的屏幕方向
int curOrientation = getRequestedOrientation();
设置全局的屏幕旋转方向
注意:如果对应AndroidManifest中设置的 screenOrientation=“sensorLandscape”,那以下方式设置屏幕旋转会没有效果
要把 AndroidManifest 中设置为 screenOrientation=“landscape”。
//先取消重力感应控制方向,0为锁定,1为可自动旋转。
Settings.System.putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION,0);
//0为正向横屏,1为反向横屏(如果对应AndroidManifest中设置的 screenOrientation="sensorLandscape",那屏幕旋转会没有效果)
Settings.System.putInt(getContentResolver(),Settings.System.USER_ROTATION, rotation); // 设置屏幕方向
try {
int rotation = Settings.System.getInt(getContentResolver(),Settings.System.USER_ROTATION); // 获取屏幕方向
Log.e("SPWEFXActivity", "屏幕方向:" + rotation);
if (rotation == 0) { // 0为正向横屏
}else { // 1为反向横屏
}
} catch (Exception e) {
e.printStackTrace();
}