提问人:moorka 提问时间:12/19/2012 最后编辑:Dr.jackymoorka 更新时间:10/27/2023 访问量:5257
为什么'android:screenOrientation=“behind”'在android 4.1.2中没有效果?
Why has `android:screenOrientation="behind"` no effect in android 4.1.2?
问:
作为测试示例,有一个包含 2 个活动的应用程序:单击按钮时启动。
它在 Android 4.0.4 上运行良好,但在 Android 4.1.2 上我遇到了意外行为。MainActivity
SecondActivity
系统设置中的自动旋转已关闭(或已打开 - 没关系,无论如何都会忽略“后面”选项)。
设置为 和 设置为 ,这意味着也必须以横向启动。
Android 4.0.4 确实如此,但在 Android 4.1.2 上从纵向开始。android:screenOrientation="landscape"
MainActivity
android:screenOrientation="behind"
SecondActivity
SecondActivity
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);
}
单击按钮后不带行的日志输出:android:configChanges="screenSize|orientation"
AndroidManifest.xml
SecondActivity
MainActivity
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
现在没有活动娱乐,但结果总是一样的 - 从纵向开始!:(
也就是说,由于某种原因,在 onResume 之后旋转为纵向。
为什么?。SecondActivity
SecondActivity
测试于:
- 三星Galaxy S3与Android 4.0.4(确定)
- 三星Galaxy S3与Android 4.1.?(错误)
- 带有纵向主屏幕方向的Android 4.1.2模拟器(错误)
- Android 4.0.3模拟器(确定)
- Android 4.2模拟器(错误)
答:
android:targetSdkVersion="16"
在清单文件中删除此语句,因为 SDKVersion=16 仅适用于 v4.0。
评论
如果您遇到使用 manifest 更改方向的问题,您可以在 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
评论
onConfigurationChanged
super
onConfigurationChanged
onResume
android:configChanges="screenSize|orientation"