zoukankan      html  css  js  c++  java
  • Android横竖屏切换及其相应布局载入问题

    第一。横竖屏切换连带载入多屏布局问题:

    假设要让软件在横竖屏之间切换。因为横竖屏的高宽会发生转换,有可能会要求不同的布局。

    能够通过下面两种方法来切换布局:

    1)在res文件夹下建立layout-land和layout-port文件夹,对应的layout文件名称不变,比方:layout-land是横屏的layout,layout-port是竖屏的layout。其它的不用管,横竖屏切换时程序调用Activity的onCreate方法中的setOnContent(xxx),并自己主动载入对应的布局。

    2)假如布局资源不依照如上设置。则能够通过java代码来推断当前是横屏还是竖屏然后来载入对应的xml布局文件。由于当屏幕变为横屏的时候,系统会又一次载入当前Activity的onCreate方法(也就是说:该Activity的生命周期要重头開始),你能够把下面方法放在你的onCreate中来检查当前的方向,然后能够让你的setContentView来载入不同的layout 。

    /** 1:竖屏   2:横屏 推断屏幕以旋转的方向 */
    	private int orientation;
    orientation=getResources().getConfiguration().orientation;
    /**
    假设使用该屏幕切换方式,则该Activity的生命周期在你切换屏幕的时刻会一直遵循:onPause()---->onStop--->onDestroy()---->onCreate()---->onStart()----->onResume(); 不会运行以下的方法:onConfigurationChanged(Configuration newConfig)
    */
    @Override
    	public void onConfigurationChanged(Configuration newConfig) {
    		// TODO Auto-generated method stub
    		super.onConfigurationChanged(newConfig);
    		Toast.makeText(getApplicationContext(), "屏幕切换了", Toast.LENGTH_SHORT).show();
    	}
    


    第二,强制设定屏幕的横、竖屏方向:

    Android横竖屏切换在手机开发中比較常见,非常多软件在开发过程中为了避免横竖屏切换时引发不必要的麻烦。通常要强制设置横竖屏的方向,

    通过在AndroidManifest.xml中设置activity中的android:screenOrientation属性值来实现。

    比方下列设置:

    横屏显示设置:android:screenOrientation="lanscape"
    
    竖屏显示设置:android:screenOrientation="portrait"

    当然上述改动也能够在Java代码中通过代码来实现:(android屏幕的切换会重新启动Activity,所以在Activity销毁前保存当前活动的状态,并在Activity再次Create的时候加载配置)

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

    第三,拦截横竖屏切换 须要的配置文件:onConfigurationChanged

    Activity每次横竖屏切换都会又一次调用onPause->onStop-> onDestory->onCreate->onStart->onResume(为此内容和数据要保存和读取,否则转屏之前的内容就会消失了)

    非常多时候这种结果让程序繁琐。为此Android提供了在manifest中设置android:configChanges属性。从而让Activity不延续上述的重建流程

    方式一)在Androidproject的Mainfest.xml配置Activity:android:configChanges="keyboardHidden|orientation横竖屏切换之后就不会去运行OnCreat函数了,而是会去调用onConfigurationChanged()这样就能控制横竖屏的切换了。

    <Android横竖屏切换不又一次调用onCreate()>可是,有一点请注意:Android2.3之前使用上面的android:configChanges="keyboardHidden|orientation就能行。可是,在4.0之后。

    必须就要使用这个属性android:configChanges="orientation|keyboardHidden|screenSize才干避免Activity又一次载入该类不延续上述的重建流程


    方式二)用户能够在Activity或View的:onConfigurationChanged(Configurationnew   Config) ,函数中获取当前横竖屏參数。

    至于其调用顺序跟touch时间的传递顺序相似,只是他没有消费事件的概念。会顺次调用到每个onConfigurationChanged函数。

    须要重写Activity的onConfigurationChanged方法。实现方式例如以下,不须要做太多的内容

    须要注意的是。onConfigurationChanged函数中仅仅能获得横竖屏切换后的參数。在该函数中获取不到新的Layout和控件的尺寸位置信息,假设要处理尺寸和位置信息,必须通过消息异步或者延时调用;

            @Override
            public void onConfigurationChanged(Configuration newConfig) {
                    super.onConfigurationChanged(newConfig);
                    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                            // land do nothing is ok
                    } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                            // port do nothing is ok
                    }
            } 
    

    第四,自适应切换屏幕:

    假设想让它启动的时候是横屏的话就横屏表示,纵屏的话就纵屏表示。然后手机切换横竖屏就不能用了该怎么解决呢?

    首先:在Mainfest.xml中追加

    android:screenOrientation="sensor" android:configChanges="orientation|keyboardHidden"

    然后:取得屏幕的长和宽,进行比較设置横竖屏的变量。

    Display display = getWindowManager().getDefaultDisplay();
    		int width = display.getWidth();
    		int height = display.getHeight();
    		if (width > height) {
    			orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; // 横屏
    		} else {
    			orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 竖屏
    		}

    然后:在onConfigurationChanged()函数中追加this.setRequestedOrientation(orientation)

    public void onConfigurationChanged(Configuration newConfig) {  
    			     super.onConfigurationChanged(newConfig);  
    			     this.setRequestedOrientation(orientation);  
    			 }  

    可是这种话你切到别的画面的时候再回到原画面。它就仍然是横的或者是纵的。

    怎么让它从别的屏幕回来后。又又一次横竖屏布局呢?

    仅仅要在OnResume()中在设定下即可了。可是这个仅仅仅仅是支持横竖屏仅仅有一个layout的;

    protected void onResume() {
    		orientation = ActivityInfo.SCREEN_ORIENTATION_USER;
    		this.setRequestedOrientation(orientation);
    		Display display = getWindowManager().getDefaultDisplay();
    		int width = display.getWidth();
    		int height = display.getHeight();
    		if (width > height) {
    			orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    		} else {
    			orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    		}
    		super.onResume();
    	}


    有三点请注意:

    1、不设置Activity的android:configChanges时。切屏会又一次调用各个生命周期,切横屏时会运行一次。切竖屏时会运行两次
    2、设置Activity的android:configChanges="orientation"时。切屏还是会又一次调用各个生命周期,切横、竖屏时仅仅会运行一次
    3、设置Activity的android:configChanges="orientation|keyboardHidden"时。切屏不会又一次调用各个生命周期。仅仅会运行onConfigurationChanged方法


    建一个群。方便大家交流:蹦蹦哒Android <群号:423923313>


  • 相关阅读:
    [转]Xml Schema
    设计模式之Observer Pattern
    通过 C# 使用 J# 类库中的 Zip 类压缩文件
    An extender can't be in a different UpdatePanel than the control it extends
    关于AutoResetEvent和ManualResetEvent
    ref, out参数区别
    取整, 无条件进位, 无条件取整
    VB.NET语法基础
    XP防火墙,挡掉访问自己的IIS
    maybe useful for Add the solution to source control
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5284773.html
Copyright © 2011-2022 走看看