// 配置android:configChanges 防止切换的时候销毁重新创建activity
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.example.oriengation.MainActivity"
android:label="@string/app_name" >
// 更改屏幕方向
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
System.out.println("方向改变了");
}
public void change(View v){
//获取当前屏幕的方向
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
//切换
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}