prevent活动再启动时的方向变化 [英] prevent activity restarting when orientation changes
问题描述
我是新android开发。我有单独的屏幕的纵向和横向mode.When我改变我的方向对应的屏幕被加载和活动将重新启动。现在,我不希望我的活动时,我改变了方向,但应加载其相应的屏幕(AXML)重新启动。
I am new to android development .I have separate screens for portrait and landscape mode.When i change my orientation corresponding screen gets loaded and activity restarts . Now i do not want my activity to restart when i change the orientation but should load its corresponding screen(axml).
我已经试过
【活动(标签= MyActivity,ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation)]
[Activity (Label = "MyActivity",ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation)]
以上线站活动得到重新启动,但它加载相同的屏幕(AXML)。 请建议。 谢谢
the above line stops activity getting restarted but it loads the same screen(axml). Please suggest . thanks
推荐答案
写这篇code。在您的活动
Write this code in your activity
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.landscapeView);
} else {
setContentView(R.layout.portraitView);
}
}
而在你的清单文件也加入这一行
And also add this line in your Manifest file
android:configChanges="orientation|keyboardHidden|screenSize"
因此,这将同时处理的事情,也不会重新启动活动,将加载布局按照您的方向改变。
So this will handle both things, it will not restart your activity and will load the layout as per your orientation changes.
这篇关于prevent活动再启动时的方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!