==Android 2.3以前的横竖屏切换==

在Android 2.3平台上,我们可以需要设置界面的横竖屏显示时,可以在AndroidManifest.xml中,对Activity的属性添加以下代码:

    android:configChanges="orientation"
同时在Activity中覆写onConfigurationChanged方法
@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);Log.i("TAG","I'm Android 2.3");}

通过设置,当前Activity在横竖屏切换的时候,便不会重新走Activity的生命周期,而是直接执行onConfigurationChanged()方法里的内容。

==Android 4.0以后的横竖屏切换==

当我们在4.0上像之前那样设置横竖屏时,会发现竟然没有效果,Activity依然走自己的生命周期,这是因为在API level 13以后Android做了修改了,SDK描述如下:

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
也就是说在Android 3.2(API level 13)以后,当设备横竖屏切换时屏幕尺寸也改变了。因此,如果你想在API Level 13或者更高的环境下,像以前那样阻止设备的横竖屏切换,你需要在orientation后加上screenSize。也就说你要像这样声明:android:configChanges="orientation|screenSize"。
也就是说我们现在要在AndroidManifest.xml中的Activity加入以下属性:
    android:configChanges="orientation|screenSize"
同时依然要在Activity中覆写onConfigurationChanged方法
@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);Log.i("TAG","I'm Android 4.0");}
参考:http://developer.android.com/guide/topics/resources/runtime-changes.html

更多相关文章

  1. Android系统开发—对View的clipChildren,clipToPadding,importan
  2. Android SDK Manager 更新方法
  3. android 实现时钟的简单方法
  4. Android 横竖屏切换的Activity生命周期演示-面试常问题
  5. android上传图片和参数(属性)到服务器
  6. Android Studio SVN 使用方法

随机推荐

  1. android webview长按识别图片 ,利用zxing
  2. 自定义键盘(一)
  3. android NDK开发 静态/动态注册 jni
  4. Android(安卓)WebView中无法用JS调用Java
  5. Android(安卓)DT/DTO镜像简介
  6. android 之popupWindow 在指定位置上的显
  7. Android如何获取SDCard 内存
  8. android 通话记录去重查询方法
  9. Android(安卓)中文 SDK (48) ―― Filter.F
  10. GIT和repo使用方法:下载内核 android源码