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

标签 android android-activity camera android-camera

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

设备:硬件 Nexus 5X

平台:Android 7.1.1

list :

<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" />

样式.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 个方向

更新:上面的回答没有解决问题。核心问题是将方向改变 180 度(即从横向到反向横向)不会触发 Activity 重新创建。这是预期的行为。通常情况下,横向布局与反向横向布局没有区别。 如果您想在旋转 180 度后更改某些 View ,可以使用 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回调

关于android - 为什么屏幕旋转 180 度不算作配置更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41898607/

相关文章:

android - 如何以编程方式将按钮添加到android中的LinearLayout

java - 尝试在 Android Studio 上启动我的第三个 Activity 时被重定向到初始 Activity

android - Xamarin.Forms 使用相机拍照显示方向错误并在后退按钮上崩溃

c++ - Opengl 鼠标相机问题 (gluLookAt)

java - 如何从另一个对象访问服务内的变量

android - Webview 上的 Telegram URL(在 Telegram 应用程序链接上共享)

Android-键盘随机显示onResume

java - 请一些 Java 帮助处理 Android 源代码?

ios - Swift Playground iPad 相机访问

android - 相机获取参数失败