为什么屏幕旋转 180 度不算作配置更改?

Why 180 degree rotation of the screen is not counted as a config change?

为什么将屏幕快速旋转 180 度不算作配置更改,因此不会触发 Activity.onCreate()!?所以我不能重新调整我的相机视图!如果旋转 90 度,每次都会触发配置更改。 我将省略源代码,因为我的清单没有 configChanges 也没有 orientation 属性。 activity 也没什么特别的。这似乎是一种预期的行为,但我没有找到任何官方信息。

设备:硬件 Nexus 5X

平台:Android7.1.1

清单:

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="false"
    android:xlargeScreens="true" />

<application
    android:name="xyz.MyApp"
    android:allowBackup="true"
    android:backupAgent=".provider.MyBackupAgent"
    android:hardwareAccelerated="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:restoreAnyVersion="true"
    android:theme="@style/AppTheme"
    android:uiOptions="splitActionBarWhenNarrow"
    tools:replace="android:icon" >

<activity android:name="xyz.MyActivity"
        android:label="asdf"
        android:theme="@style/Theme" />

styles.xml:

<style name="Theme" parent="Theme.AppCompat.NoActionBar">
    <item name="actionBarIconColor">#fff</item>
    <item name="actionBarInsetStart">@dimen/keyline_2</item>
</style>

样式-v19.xml:

<style name="Theme" parent="Theme.AppCompat.NoActionBar">
    <item name="actionBarIconColor">#fff</item>
    <item name="actionBarInsetStart">@dimen/keyline_2</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

您应该在 AndroidManifest.xml 中为您的 activity 指定 android:screenOrientation="fullSensor" 正如所描述的 here 默认方向是 sensor 忽略反向纵向方向。 fullSensor 允许所有 4 个方向

UPDATE:上面的回答没有解决问题。核心问题是将方向改变 180 度(即从横向到反向横向)不会触发 activity re-creating。这是预期的行为。通常情况下,横向布局与反向横向布局没有区别。 如果您想在旋转 180 度后更改某些视图,可以使用 OrientationEventListener

样本:

final WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

OrientationEventListener orientationEventListener = new OrientationEventListener(this,
                SensorManager.SENSOR_DELAY_NORMAL) {
            @Override
            public void onOrientationChanged(int orientation) {
                if (orientation == ORIENTATION_UNKNOWN) {
                    return;
                }
                Display display = mWindowManager.getDefaultDisplay();
                int rotation = display.getRotation();
                if (rotation != mLastRotation) {
                    // do something with your views
                    mLastRotation = rotation;
                }
            }
        };

 if (orientationEventListener.canDetectOrientation()) {
     orientationEventListener.enable();
 }

如果你的 minSdk 是 17+ 你也可以使用 DisplayListener#onDisplayChanged 回调

请在 AndroidManifest 文件中使用:

android:screenOrientation="sensorLandscape"