21天学习挑战赛--第四天打卡(横竖屏切换)

1.横竖屏切换

Android横竖屏要解决的问题应该就两个:

①布局问题

②重新载入问题

③生命周期问题

2.布局问题

①如果不想让app在横竖屏之间切换,可以在AndroidManifest.xml中指定的activity中加上android:screenOrientation属性,有以下几个参数:

"unspecified":默认值 由系统来判断显示方向。判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.。

"landscape":横屏显示

"portrait":竖屏显示

"user":用户当前首选的方向 

"behind":和该Activity下面的那个Activity的方向一致(在Activity堆栈中的) 

"sensor":由物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。 

"nosensor":忽略物理感应器,这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。

也可以在Java代码中通过setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)来设置。

②如果要让app在横竖屏之间切换,由于横竖屏的高宽会发生转换,有可能会要求不同的布局。可以通过以下方法来切换布局:

1)在res目录下建立layout-land和layout-port目录,相应的layout文件不变,比如main.xml。layout-land目录下是横屏的layout,layout-port是竖屏的layout,其他的不用管,模拟器会自动寻找。

2)通过this.getResources().getConfiguration( ).orientation判断当前是横屏还是竖屏,然后加载相应的xml布局文件。因为当屏幕变为横屏的时候,系统会重新呼叫当前Activity的onCreate方法,因此可以在onCreate中检查当前的方向,然后让setContentView来载入不同的layout xml。

if (this.getResources().getConfiguration( ).orientation == Configuration.ORIENTATION_LANDSCAPE){

    Log.i("info","landscape"); // 横屏

} else if(this.getResources().getConfiguration( ).orientation ==Configuration.ORIENTATION_PORTRAIT) {

    Log.i("info","portrait"); // 竖屏

}

3.重新载入问题

①如果不需要重新载入,可以在AndroidManifest.xml中加入配置 android:configChanges="orientation|keyboardHidden"。配置configChanges的作用就是如文档所说的:Specify one or more configuration changes that the activity will handle itself. If not specified, the activity will berestarted if any of these configuration changes happen in the system。这样在程序中Activity就不会重复的调用onCreate()甚至不会调用onPause、onResume。只会调用一个onConfigurationChanged(Configuration newConfig)。

②如果需要重新载入,则不需要做任何修改。不过如果需要在重新载入过程中保存之前的操作内容或数据,则需要保存之前的数据。然后在 activity的onCreate()中取出来。当然,如此就不能设置android:configChanges()了,否则就不会调用 onCreate()方法。

3.生命周期问题

测试横竖屏切换时activity的生命周期:

①新建一个Activity,把各个生命周期打印出来,运行Activity,得到如下信息

onCreate-->

onStart-->

onResume-->

②按切换成横屏时

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

③再切换成竖屏时,发现打印了两次相同的log

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

④修改AndroidManifest.xml,把该Activity添加 android:configChanges="orientation",执行切换到横屏

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

⑤再执行切换到竖屏,发现不会再打印相同信息,但多打印了一行onConfigChanged

onSaveInstanceState-->

onPause-->

onStop-->

onDestroy-->

onCreate-->

onStart-->

onRestoreInstanceState-->

onResume-->

onConfigurationChanged-->

⑥把android:configChanges="orientation" 改成 android:configChanges="orientation|keyboardHidden",执行切换到横屏,就只打印onConfigChanged

onConfigurationChanged-->

⑦执行切换到竖屏

onConfigurationChanged-->

onConfigurationChanged-->

所以,可以得到结论:

①不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次。

②设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次。

③设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法。

猜你喜欢

转载自blog.csdn.net/zenmela2011/article/details/126234736