zoukankan      html  css  js  c++  java
  • 关于activity横竖屏切换的问题

    界面的横竖屏切换

    1、在androidmanifest.xml中对所需要进行横竖屏切换的activity进行设置以下属性:

      android:screenOrientation="portrait"

    当前设置为只可以进行竖屏的显示;

      android:screenOrientation="landscape"

    当前设置可以进行横屏显示;

    2、横竖屏切换时生命周期变化

      

    3、Android 2.3以前的横竖屏切换

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

        	android:configChanges="orientation"
    

    同时在Activity中覆写onConfigurationChanged方法

    	@Override
    	public void onConfigurationChanged(Configuration newConfig) {
    		super.onConfigurationChanged(newConfig);
    		
    	}
    

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

    4、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方法

    	@Override
    	public void onConfigurationChanged(Configuration newConfig) {
    		super.onConfigurationChanged(newConfig);
    		
    	}
    这样的设置之后就可以阻止activity被销毁,进而可以不用在横竖屏切换的时候进行activity的创建,避免不必要的麻烦。
    ()

  • 相关阅读:
    网络基础知识-TCP/IP协议各层详解
    MySQL及其图形界面navicat的安装
    Python 浅谈编程规范和软件开发目录规范的重要性
    python 浅析模块,包及其相关用法
    spring batch中MyBatisPagingItemReader分页使用介绍
    eclipse中git插件使用
    oracle中查找某用户执行某张表的操作操作记录
    redis集群主流架构方案分析
    消息队列常见的 5 个应用场景
    Kafka vs RocketMQ——单机系统可靠性
  • 原文地址:https://www.cnblogs.com/will-peng/p/check.html
Copyright © 2011-2022 走看看