12

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).

I have tried

[Activity (Label = "MyActivity",ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation)]

the above line stops activity getting restarted but it loads the same screen(axml). Please suggest . thanks

4

2 回答 2

41

在您的活动中编写此代码

@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);
    }
}

并在您的清单文件中添加这一行

android:configChanges="orientation|keyboardHidden|screenSize"

所以这将处理这两件事,它不会重新启动您的活动,并将根据您的方向更改加载布局。

于 2013-07-30T11:44:14.057 回答
5

由于您已向操作系统指定要自己处理方向更改,因此现在您必须自己处理对布局的任何更改,如下所示:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig); 

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        setContentView(R.layout.portrait);
        //do other initialization
    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setContentView(R.layout.landscape);
        //do other initialization
    }
}
于 2013-07-30T11:10:04.200 回答