您的位置:首页 > 移动开发 > Android开发

Android横屏竖屏切换总结

2012-02-10 00:43 447 查看
1.切换模拟器的横竖屏的方式:快捷键:ctrl+f12
如果固定横屏可以在eclipse 里面run dialog->target 里面可以设置.如果是命令行可以使用参数

emulator -skin HVGA-L

2.要让程序界面保持一个方向,不随手机方向转动而变化的处理办法:

在AndroidManifest.xml里面配置一下就可以了。加入这一行android:screenOrientation="landscape"。

例如(landscape是横向,portrait是纵向):

另外,android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置,那样,进行中的游戏就不会自动重启了!
有的程序适合从竖屏切换到横屏,或者反过来,这个时候怎么办呢?可以在配置Activity的地方进行如下的配置android:screenOrientation="portrait"。这样就可以保证是竖屏总是竖屏了,或者landscape横向。

3. 程序中如果横竖屏的变换可以变换样式,如背景图片,按钮大小和布局等。所以可以利用Android系统每次屏幕切换会重启Activity的特性,在OnCreat处自动加载不同显示状态下的layout。

实现有如下几种方式:

(1)手动变换资源文件等操作。即是因为屏幕变换后会再次重启而调用oncreate(),所以判断当前的屏幕为横或竖方向后更改资源文件即可实现。
方法如下:

[java]
view plaincopyprint?

//获得当前的屏幕方向 
    public static
int ScreenOrient(Activity activity){ 

        //取得当前屏幕的方向,如果此值为-1表示androidManifest.xml没有设置Android:screanOrentation属性所以这样无法判断屏幕方向。 

        //可以使用另一种思路,即长度大于高度的为横屏,否则为竖屏。 

        int orientation = activity.getRequestedOrientation();//得到屏幕方向 

        int landscape = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;//横屏静态常量 

        int portrait = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;//竖屏常量 

        int width = activity.getWindowManager().getDefaultDisplay().getWidth();//得到系统显示属性后得到屏幕宽度 

        int height = activity.getWindowManager().getDefaultDisplay().getHeight();//得到屏幕高度 

        return width>height?portrait:landscape;//判断 

    } 

[c-sharp]
view plaincopyprint?

@Override 
        public void onConfigurationChanged(Configuration newConfig) { 

                super.onConfigurationChanged(newConfig); 

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

                        // land do nothing is ok 

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

                        // port do nothing is ok 

                } 
        } 

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


其实在这种模式下,由于Android系统自动寻找不同界面文件,而Androidmanifest.xml中已定义了android:configChanges="keyboardHidden|orientation"只监听方向改变
所以我们在onConfigurationChanged里改变应该改变的东西即可。
 
 
原文地址:http://blog.csdn.net/leesidong/article/details/6307375
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息