安卓横竖屏切换改变布局文件1
开启安卓的自动旋转,在改变方向后,自动更改布局文件,其中activity会重启,数据不会保存,需要后面进行数据保存恢复
在AndroidManifest添加
android:configChanges="keyboardHidden|screenSize|orientation"
正常横竖屏切换的生命周期:
onPause()->onSaveInstanceState()-> onStop()->onDestroy()->onCreate()->onStart()->onRestoreInstanceState->onResume()
- 不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次
- 设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次
- 设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法
新建横屏布局
- 切换到Project
2.在res目录下新建
2. 选择layout->Orientation
3. 选择横屏
4.然后将原布局复制或新建同名文件在layout,此时就会出现两个布局文件
5. 初步能实现布局的切换,但是文件的布局不同时会出现显示不正确,此时我们需要在Java文件中添加
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
}
竖屏布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:gravity="end"
android:layout_height="60dp"
android:focusableInTouchMode="false"
android:hint="0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/clear"
android:layout_width="80dp"
android:layout_height="60dp"
android:textSize="20sp"
android:text="c"/>
<Button
android:id="@+id/del"
android:layout_width="80dp"
android:layout_height="60dp"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:text="del"/>
<Button
android:id="@+id/division"
android:layout_width="80dp"
android:layout_height="60dp"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:text="/"/>
<Button
android:id="@+id/mult"
android:layout_width="80dp"
android:layout_height="60dp"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:text="*"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/bt1"
android:layout_width="80dp"
android:layout_height="60dp"
android:textSize="20sp"
android:text="1"/>
<Button
android:id="@+id/bt2"
android:layout_width="80dp"
android:layout_height="60dp"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:text="2"/>
<Button
android:id="@+id/bt3"
android:layout_width="80dp"
android:layout_height="60dp"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:text="3"/>
<Button
android:id="@+id/sub"
android:layout_width="80dp"
android:layout_height="60dp"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:text="-"/>
</LinearLayout