首先在清单文件加权限:<uses-permission android:name="android.permission.CHANGE_CONFIGURATION">
然后重写onConfigurationChanged()方法.
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//横向
setContentView(R.layout.main_landscape);
//重新实例化组件和设置监听
}else{
//竖向
setContentView(R.layout.main);
//重新实例化组件和设置监听
}
}
最后别忘了在onCreate()时加上判断,
if(getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.main_landscape);
}else {
setContentView(R.layout.main);
}