【问题标题】:Why has `android:screenOrientation="behind"` no effect in android 4.1.2?为什么 `android:screenOrientation="behind"` 在 android 4.1.2 中不起作用?
【发布时间】:2012-12-06 23:53:18
【问题描述】:

作为测试样本,有一个应用程序具有 2 个活动:MainActivity,在单击按钮时启动 SecondActivity
它在 Android 4.0.4 上运行良好,但在 Android 4.1.2 上我遇到了意外行为。

系统设置中的 AutoRotation 已关闭(或已打开 - 没关系,“后面”选项无论如何都会被忽略)。
android:screenOrientation="landscape" 设置为 MainActivityandroid:screenOrientation="behind" 设置为 @987654327 @,这意味着SecondActivity也必须横向启动。
对于 Android 4.0.4 是这样,但在 Android 4.1.2 上 SecondActivity 以纵向开始。

AndroidManifest.xml:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.my.example.testbehindorientation.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.my.example.testbehindorientation.SecondActivity"
        android:configChanges="screenSize|orientation"
        android:label="@string/title_activity_second"
        android:screenOrientation="behind" >
    </activity>
</application>

SecondActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    logOrientation("onCreate");
}


@Override
protected void onDestroy() {
    super.onDestroy();
    logOrientation("onDestroy");
}

@Override
protected void onResume() {
    super.onResume();
    logOrientation("onResume");
}

private void logOrientation(String prefix) {
        int requestedOrientation = this.getRequestedOrientation();
        WindowManager lWindowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
        Configuration cfg = getResources().getConfiguration();
        int lRotation = lWindowManager.getDefaultDisplay().getRotation();   
        int orientation = cfg.orientation;
        Log.i(LOG_TAG, prefix + ", requestedOrientation is " + requestedOrientation + ", rotation is " + lRotation + ", orientation is " + orientation);
}

MainActivity 中单击按钮后SecondActivityAndroidManifest.xml 中没有android:configChanges="screenSize|orientation" 行的日志输出:

onCreate, requestedOrientation is 3, rotation is 1, orientation is 2
onResume, requestedOrientation is 3, rotation is 1, orientation is 2
onDestroy, requestedOrientation is 3, rotation is 0, orientation is 1
onCreate, requestedOrientation is 3, rotation is 0, orientation is 1
onResume, requestedOrientation is 3, rotation is 0, orientation is 1

使用android:configChanges="screenSize|orientation" 包含在AndroidManifest.xml 中的行记录:

onCreate, requestedOrientation is 3, rotation is 1, orientation is 2
onResume, requestedOrientation is 3, rotation is 1, orientation is 2

现在没有活动娱乐,但结果总是一样的 - SecondActivity 是从纵向开始的! :( 也就是说,SecondActivity 在 onResume 之后由于某种原因旋转为纵向。 为什么?

测试于:

  • 搭载 Android 4.0.4 的三星 Galaxy S3(正常)
  • 三星 Galaxy S3 与 Android 4.1.? (错误)
  • 带有 Android 4.1.2 的模拟器,带有纵向主屏幕方向(错误)
  • Android 4.0.3 模拟器(好的)
  • Android 4.2 模拟器(错误)

【问题讨论】:

  • 你有onConfigurationChanged处理程序调用super吗?
  • @Stan 我可以添加onConfigurationChanged,但它无济于事。它只让我看到onResume 之后以新的纵向方向调用它的那一刻(如果行android:configChanges="screenSize|orientation" 出现在AndroidManifest.xml 中)
  • 有一个打开的错误报告here
  • 当系统设置中的 AutoRotation 为 ON 时,它的行为是否符合预期?
  • @forgivegod 不,它没有。如果自动旋转开启,SecondActivity 会忽略“behind”属性并随着手机屏幕旋转。

标签: android android-activity orientation screen-orientation android-orientation


【解决方案1】:

如果您在使用清单更改方向时遇到问题,您可以在 java 中更改方向。这是清单和活动的完整代码。

Manifest.xml

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.my.example.testbehindorientation.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.my.example.testbehindorientation.SecondActivity"
        android:configChanges="screenSize|orientation"
        android:label="@string/title_activity_second" >
    </activity>
</application>

横向的第二个活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);            
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//Change according to need this is for landscape
    logOrientation("onCreate");
}


@Override
protected void onDestroy() {
    super.onDestroy();
    logOrientation("onDestroy");
}

@Override
protected void onResume() {
    super.onResume();
    logOrientation("onResume");
}

private void logOrientation(String prefix) {
        int requestedOrientation = this.getRequestedOrientation();
        WindowManager lWindowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
        Configuration cfg = getResources().getConfiguration();
        int lRotation = lWindowManager.getDefaultDisplay().getRotation();   
        int orientation = cfg.orientation;
        Log.i(LOG_TAG, prefix + ", requestedOrientation is " + requestedOrientation + ", rotation is " + lRotation + ", orientation is " + orientation);
}

在MainActivity的onCreate()方法中添加如下代码

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//Change according to need this is for landscape

【讨论】:

    【解决方案2】:

    android:targetSdkVersion="16"

    在清单文件中删除此语句,因为 SDKVersion=16 仅适用于 v4.0。

    【讨论】:

    • 顺便说一下,SDKVersion = 16 代表 Android 4.1。无论如何,删除或保留此声明无济于事。
    • 目标 sdk 版本是您构建时使用的版本。它完全允许它是与其运行的设备不同的版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2013-06-20
    相关资源
    最近更新 更多