33

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

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

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>

第二活动.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);
}

单击按钮后,不带android:configChanges="screenSize|orientation"行的AndroidManifest.xml记录输出:SecondActivityMainActivity

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之后由于某种原因旋转到纵向。为什么?。

测试:

  • 三星 Galaxy S3 与 Android 4.0.4 (好的)
  • 三星 Galaxy S3 与 Android 4.1。?(漏洞)
  • 带有 Android 4.1.2 的模拟器,带有纵向主屏幕方向(错误)
  • Android 4.0.3 模拟器(好的)
  • Android 4.2 模拟器(错误)
4

2 回答 2

0

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

清单.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
于 2021-07-16T05:54:36.770 回答
-1

android:targetSdkVersion="16"

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

于 2013-01-03T07:36:06.713 回答