作为一个测试样本,有一个包含两个活动的应用程序:单击按钮时启动SecondActivity
的MainActivity
。
它在Android 4.0.4上运行得很好,但在Android 4.1.2上我遇到了意想不到的行为。
系统设置中的AutoRotation处于关闭状态(或处于打开状态-这无关紧要,但忽略"behind“选项)。
android:screenOrientation="landscape"
是为MainActivity
设置的,android:screenOrientation="behind"
是为SecondActivity
设置的,这意味着SecondActivity
也必须以横向启动。
对于安卓4.0.4来说也是如此,但是在安卓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
中单击按钮后,在AndroidManifest.xml
中为SecondActivity
记录不带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
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
现在没有活动娱乐,但结果总是一样的- SecondActivity
从纵向开始!:(也就是说,由于某种原因,SecondActivity
在onResume之后旋转到纵向。为什么?。
测试对象:
发布于 2021-07-16 13:54:37
如果您在使用清单更改方向时遇到问题,您可以在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);
}
在 onCreate() MainActivity的方法中添加以下代码
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//Change according to need this is for landscape
发布于 2013-01-03 15:36:07
android:targetSdkVersion="16"
在清单文件中删除此语句,因为SDKVersion=16仅适用于v4.0。
https://stackoverflow.com/questions/13955606
复制相似问题