關於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;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章