1.在AndroidManifest文件里配置 android:configChanges="keyboardHidden|screenSize|orientation"> 使其横竖屏切换时不会重启整个activtiy:
<activity android:name=".MainActivity">
android:configChanges="keyboardHidden|screenSize|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2.然后创建两个xml文件 分别对应横屏和竖屏状态
3.在MainActivity中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_main_horizontal);
}else{
setContentView(R.layout.activity_main_vertical);
}
}
|