项目中
activity的定义:
<activityandroid:name=".activity.EventsActivity"/>
这样定义后每次横竖屏切换都需要重新onCreate() activity (MyActivity extends BaseActivity)
在BaseActivity.java的onCreate()方法中判断:
代码1:
if (this.getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
isLandscape = true;
System.out.println("<<<<<<<<<<<<<<<<<< This is in Landscape mode... ");
}else if (this.getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){ // portrait
isLandscape = false;
System.out.println("<<<<<<<<<<<<<<<<<< This is in Portrait mode... ");
}else{
System.out.println("<<<<<<<<<<<<<<<<<< This is in else mode... ");
}
结果:
从竖屏切换到横屏: 06-12 12:38:14.256: I/System.out(5324): <<<<<<<<<<<<<<<<<< This is in else mode...
再从横屏切换到竖屏: 06-12 12:38:55.946: I/System.out(5324): <<<<<<<<<<<<<<<<<< This is in Portrait mode...
再切依次往复
代码2:
if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
isLandscape =true;
System.out.println("<<<<<<<<<<<<<<<<<< This is in Landscape mode... ");
}else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){ // portrait
isLandscape =false;
System.out.println("<<<<<<<<<<<<<<<<<< This is in Portrait mode... ");
}else{
System.out.println("<<<<<<<<<<<<<<<<<< This is in else mode... ");
}
结果:
从竖屏切换到横屏: 06-12 12:38:14.256: I/System.out(5324): <<<<<<<<<<<<<<<<<< This is in else mode...
再从横屏切换到竖屏: 06-12 12:38:55.946: I/System.out(5324): <<<<<<<<<<<<<<<<<< This is in else mode...
再切还是同样的结果!
代码3:
if(height < width){
isLandscape =true;
System.out.println("<<<<<<<<<<<<<<<<<< This is in Landscape mode... ");
}else{
isLandscape =false;
System.out.println("<<<<<<<<<<<<<<<<<< This is in Portrait mode... ");
}
结果:
从竖屏切换到横屏: 06-12 12:45:26.838: I/System.out(5756): <<<<<<<<<<<<<<<<<< This is in Landscape mode...
再从横屏切换到竖屏: 06-12 12:45:31.092: I/System.out(5756): <<<<<<<<<<<<<<<<<< This is in Portrait mode...
再切依次往复
总结:第三种代码满足我的要求!
注:
/**
* Constant corresponding to <code>unspecified</code> in
* the {@link android.R.attr#screenOrientation} attribute.
*/
publicstaticfinalintSCREEN_ORIENTATION_UNSPECIFIED = -1;
/**
* Constant corresponding to <code>landscape</code> in
* the {@link android.R.attr#screenOrientation} attribute.
*/
publicstaticfinalintSCREEN_ORIENTATION_LANDSCAPE = 0;
/**
* Constant corresponding to <code>portrait</code> in
* the {@link android.R.attr#screenOrientation} attribute.
*/
publicstaticfinalintSCREEN_ORIENTATION_PORTRAIT = 1;
/**
* Constant corresponding to <code>user</code> in
* the {@link android.R.attr#screenOrientation} attribute.
*/
publicstaticfinalintSCREEN_ORIENTATION_USER = 2;
/**
* Constant corresponding to <code>behind</code> in
* the {@link android.R.attr#screenOrientation} attribute.
*/
publicstaticfinalintSCREEN_ORIENTATION_BEHIND = 3;
/**
* Constant corresponding to <code>sensor</code> in
* the {@link android.R.attr#screenOrientation} attribute.
*/
publicstaticfinalintSCREEN_ORIENTATION_SENSOR = 4;
/**
* Constant corresponding to <code>sensor</code> in
* the {@link android.R.attr#screenOrientation} attribute.
*/
publicstaticfinalintSCREEN_ORIENTATION_NOSENSOR = 5;