2012-11-22 38 views
0

如何得到它?在我的XML文件中,我为我的活动使用唯一的纵向方向。如何在不使用加速度计的情况下获取屏幕方向?

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout" 
android:screenOrientation="portrait" 
android:windowSoftInputMode="stateHidden" 

and onConfigurationChanged并且不适用于方向。

UPD目前我使用:

@Override 
    public void onSensorChanged(SensorEvent event) { 
     x = event.values[0]; 
     y = event.values[1]; 
    } 

    private PictureOrientation getLastOrientation() { 
     MyPictureOrientation orient; 
     if (x >= -5 && x < 5) { 
      if (y > 0) { 
       orient = PORTRAIT; 
      } else { 
       orient = PORTRAIT_REV; 
      } 
     } else { 
      if (x >= -10 && x < -5) { 
       orient = LAND_RIGHT; 
      } else { 
       orient = LAND_LEFT; 
      } 
     } 

     return orient; 
    } 

回答

0

您可以使用下面的代码来获得当前方向:

getResources().getConfiguration().orientation 
+0

getResources()getConfiguration()方向 - 永远== 1 – monyag

0

一些伪代码:

Display getOrient = getWindowManager().getDefaultDisplay(); 
if(getOrient.getWidth() < getOrient.getHeight()) 
    // then you have portrait orientation 
else 
    // you have landscape 
+0

不适合我。 W和H为纵向不能更改 – monyag

0

如果你想你在potrait中工作的应用程序只在清单中加入以下内容

android:configChanges="keyboardHidden|orientation" 
android:screenOrientation="portrait" 

和OnCreate中和onConfigurationChanged把

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
+0

android 4.0.4和LG optimus之一。没有工作:( – monyag

+0

页面是否改为横向? 移除后尝试 android:configChanges =“keyboardHidden | orientation” – almuneef

相关问题