我有以下问题。我的程序是秒表。我有res/layout/main.xml
,res/layout-land/main.xml
但它工作错了。当我转动手机时,程序停止。我认为它再次调用 onCreate 。我在清单中添加了以下行
android:configChanges="keyboardHidden|orientation"
但是在此布局活动未加载之后。
我已经尝试了下一个示例,但它不起作用
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int ot = getResources().getConfiguration().orientation;
switch (ot) {
case Configuration.ORIENTATION_LANDSCAPE:
setContentView(R.layout.main_land);
break;
case Configuration.ORIENTATION_PORTRAIT:
setContentView(R.layout.main);
break;
}
Toast.makeText(this, "Helloo", Toast.LENGTH_SHORT).show();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
int ot = getResources().getConfiguration().orientation;
switch (ot) {
case Configuration.ORIENTATION_LANDSCAPE:
setContentView(R.layout.main_land);
break;
case Configuration.ORIENTATION_PORTRAIT:
setContentView(R.layout.main);
break;
}
}
@Override
public Object onRetainNonConfigurationInstance() {
// TODO Auto-generated method stub
return super.onRetainNonConfigurationInstance();
}