onConfigurationChanged问题 [英] onConfigurationChanged problems
问题描述
我需要改变我的Android(4.1 API 16),当方向改变应用程序的布局。
I need to change layout of my Android (4.1 API 16) application when orientation is changed.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main_2);
System.out.println("Orientation changed!");
}
此外,我已经添加了下一行清单文件
Also I have added the next line to manifest file
android:configChanges="orientation"
我使用Ctrl + F11来改变方向。我的屏幕旋转,但布局保持一致,并没有什么印在LogCat中。感觉就像不会发生onConfigChanged事件。
I use Ctrl+F11 to change orientation. My screen rotates but layout stays the same and nothing is printed in LogCat. Feels like onConfigChanged event doesn't occur.
哪里是我的错?
感谢您。
推荐答案
尝试使用 的android:configChanges =方向| keyboardHidden |屏幕尺寸
Try using android:configChanges="orientation|keyboardHidden|screenSize"
注意:与Android 3.2(API等级13),在屏幕尺寸开始
当设备纵向和横向之间切换也改变
方向。因此,如果你想prevent运行时重新启动因
对于API级别13或更高的发展方向变化时(如
由申报的minSdkVersion和targetSdkVersion属性),你
必须包含除在屏幕尺寸价值的方向
值。也就是说,你必须decalare
的android:configChanges =方向|屏幕尺寸。但是,如果您
应用程序的目标API级别12或更低,那么你总是活动
处理此配置变化本身(此配置更改
在Android 3.2或运行时不会重新启动您的活动,甚至
更高的设备)。
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
来源:文档
因此,还添加了 |屏幕尺寸来configChanges
如果您的应用程序的目标的 API 13及以上
Hence, also add "|screenSize" to configChanges
if your application targets API 13 and above.
这篇关于onConfigurationChanged问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!