2012-07-20 56 views
5

当我将Android手机颠倒时,我的Activity不会旋转以颠倒显示布局,而是保持横向模式。我尝试了一个非常简单的HelloWorld应用程序。我在清单中添加了android:configChanges="orientation"并在活动中覆盖了onConfigurationChange()以在那里设置断点。颠倒旋转设备会产生一次配置更改,从纵向(颠倒)转到横向,但不会从横向转换为纵向(颠倒)。这是Android的问题还是有我需要做的事情?颠倒旋转不会触发配置更改

清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="hello.world" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <uses-sdk android:minSdkVersion="10" /> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:configChanges="orientation" 
      android:name=".HelloWorldActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

活动:

public class HelloWorldActivity 
    extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    } 

    public void onConfigurationChanged(Configuration newConfig) 
    { 
    super.onConfigurationChanged(newConfig); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    { 
     Log.e("MDO", "orientation change: landscape"); 
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 
    { 
     Log.e("MDO", "orientation change: portrait"); 
    } 
    } 
} 
+1

你确定你的设备做到了吗?我很少看到允许这样的应用。 – DeeV 2012-07-20 19:09:17

+0

我还没有看到这种行为,我尝试了一些标准的应用程序(设置,浏览器等),但它可能与'android:screenOrientation =“fullSensor”' – 2012-07-20 20:44:38

回答

7

这不是问题,它是Android的工作原理。它只能在横向模式下观看颠倒视角,而不是在肖像模式下(如果我没有记错的话,可以从2.2版横向观看)。当配置从纵向变为横向时发生配置更改,反之亦然。如果您想检查手机是否翻转过来,或者无论朝哪个方向,都必须访问加速计传感器。这里有一个tutorial关于如何使用它,这里你有SensorManager docs

编辑:作为问题笔者发现了自己,增加android:screenOrientation="fullSensor"你的清单是不够的,前提是你不希望任何支持年龄超过了Android 2.3(API等级9)

+0

你有一个链接到Android文档描述这? – 2012-07-20 20:01:27

+2

其实,如果我添加android:screenOrientation =“fullSensor”,我确实得到了期望的行为。 – 2012-07-20 20:06:56

+0

问题是,它不会在2.3以前的Android上运行(API级别9)。或者我错了?你测试过了吗? – 2012-07-20 20:49:05

3

包括屏幕尺寸在你mafifest您configChanges:

android:configChanges="orientation|screenSize" 

http://developer.android.com/guide/topics/resources/runtime-changes.html

从Android 3.2(API级别13)开始,当设备在纵向和横向之间切换时,“屏幕大小”也会改变。因此,如果要在开发API级别13或更高级别时(由minSdkVersion和targetSdkVersion属性声明)防止由于方向更改导致运行时重新启动,则除“方向”值外,还必须包含“screenSize”值。也就是说,你必须decalare android:configChanges =“orientation | screenSize”。但是,如果您的应用程序的目标级别为12或更低,则您的活动始终会自行处理此配置更改(即使在Android 3.2或更高版本的设备上运行,此配置更改也不会重新启动您的活动)。

3

设置android:screenOrientation="fullSensor"完成了我想要做的事情。