a . 配置文件Manifest中给activity加 android:configChanges="orientation|keyboardHidden"可以防止重新加载activity,
加android:screenOrientation="portrait"可强制竖屏
b . android:configChanges="orientation|keyboardHidden|locale|screenSize" 4.0以上需要 加locale属性才能横竖屏切换
c . android:screenOrientation="user" 加此属性后 可在代码中实现 横竖屏的代码设置
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> 此权限允许用户代码修改横竖屏setting权限
代码设定横竖屏权限:
Settings.System.putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION,0); 不允许横竖屏切换
Settings.System.putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION,1); 允许横竖屏切换
d . 重写public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
}else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
}
} 可实现横竖屏后操作
加android:screenOrientation="portrait"可强制竖屏
b . android:configChanges="orientation|keyboardHidden|locale|screenSize" 4.0以上需要 加locale属性才能横竖屏切换
c . android:screenOrientation="user" 加此属性后 可在代码中实现 横竖屏的代码设置
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> 此权限允许用户代码修改横竖屏setting权限
代码设定横竖屏权限:
Settings.System.putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION,0); 不允许横竖屏切换
Settings.System.putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION,1); 允许横竖屏切换
d . 重写public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
}else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
}
} 可实现横竖屏后操作