为什么屏幕180度旋转不被视为配置更改?

3
为什么快速旋转屏幕180度不被视为配置更改,因此不会触发Activity.onCreate()!?所以我无法重新定位我的相机视图!如果你每次旋转90度,它都会触发配置更改。我将省略源代码,因为我的清单中没有configChanges 也没有orientation属性。这个活动也没有什么特别的地方。似乎这是一种预期的行为,但我找不到任何官方信息。
设备:硬件Nexus 5X 平台:Android 7.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>

styles-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>

因为旋转180度可以保持它处于竖屏(或横屏)模式,所以不需要任何操作。 - Gabe Sechan
好的,但它会使相机预览倒置! - WindRider
2个回答

4
你应该在AndroidManifest.xml中的活动中指定android:screenOrientation="fullSensor"。如此处所述,默认方向是sensor,它忽略反向纵向方向。 fullSensor允许所有4个方向。 更新:上面的答案无法解决问题。核心问题是180度更改方向(即从横向到反向横向)不会触发活动重新创建。这是预期的行为。在通常情况下,横向布局与反向横向布局没有区别。如果要在旋转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回调函数。

@WindRider,你能否更新你的问题并附上你的活动和AndroidManifest.xml的代码? - nnesterov
我非常确定您可以在任何活动中重现它。我已经在其他活动和其他应用程序中尝试过了。这似乎是平台预期的行为。 - WindRider
1
@WindRider 请检查您的清单文件。我制作了一个示例应用程序,其中包含具有“android:screenOrientation =”fullSensor“”属性的活动。在安卓7.1.1上的Nexus 5x上按预期工作(所有4个方向)。 - nnesterov
所有4个方向都不是问题。问题出在快速翻转上下颠倒的情况。我刚刚在问题中添加了一些XML。 - WindRider
当然了。我在 MyActivity.onCreate() 上设置了断点,但没有命中! - WindRider
显示剩余2条评论

3

请在AndroidManifest文件中使用以下内容:

android:screenOrientation="sensorLandscape"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接