倒置旋转不会触发配置更改

时间:2012-07-20 19:06:15

标签: android

当我将Android手机颠倒时,我的“活动”不会旋转以显示布局颠倒,而是保持横向模式。我用一个非常简单的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");
    }
  }
}

3 个答案:

答案 0 :(得分:8)

这不是问题,而是Android的运作方式。它只能在横向模式下观看,而不是纵向观看(如果我没记错的话,可以在版本2.2中查看)。从纵向移动到横向时发生配置更改,反之亦然。如果您想检查手机是否倒置或无论您需要访问加速计传感器的方向。这是关于如何使用它的tutorial,在这里你有SensorManager docs

编辑:正如问题的作者自己发现的那样,将<{1}}添加到您的清单就足够了,只要你不想支持比Android 2.3更早的版本(API级别9)< /强>

答案 1 :(得分:3)

在你的mafifest中的configChanges中包含screenSize:

android:configChanges="orientation|screenSize"

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

  

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

答案 2 :(得分:3)

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