作为一个测试示例,有一个应用程序有 2 个 Activity :MainActivity
,它在按钮单击时启动 SecondActivity
。
它在 Android 4.0.4 上运行良好,但在 Android 4.1.2 上我遇到了意外行为。
系统设置中的 AutoRotation 已关闭(或已打开 - 没关系,“后面”选项无论如何都会被忽略)。
android:screenOrientation="landscape"
设置为 MainActivity
并且 android:screenOrientation="behind"
设置为 SecondActivity
,这意味着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);
}
AndroidManifest.xml
中没有 android:configChanges="screenSize|orientation"
行的日志输出,用于 SecondActivity
在 中单击按钮后主 Activity
:
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
使用 AndroidManifest.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
现在没有 Activity 重新创建,但结果始终相同 - SecondActivity
以纵向开始! :(
也就是说,SecondActivity
在onResume之后由于某种原因旋转为纵向。
为什么?
测试于:
- 搭载 Android 4.0.4 的三星 Galaxy S3(正常)
- 搭载 Android 4.1 的三星 Galaxy S3。? (错误)
- 带有 Android 4.1.2 的模拟器,带有纵向主屏幕方向(错误)
- Android 4.0.3 模拟器(好的)
- Android 4.2 模拟器(错误)
最佳答案
如果您在使用 list 更改方向时遇到问题,您可以在 java 中更改方向。这是 list 和 Activity 的完整代码。
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>
横向的第二个 Activity
@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
关于android - 为什么 `android:screenOrientation="后面"` 在 android 4.1.2 中没有效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13955606/