关于Android的横竖屏切换的判断

项目中

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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值