写在20110721:横竖屏切换

对于横竖屏切换的问题,大概的解决办法是:
1.只竖屏显示
android:screenOrientation="portrait"
只横屏显示
android:screenOrientation="landscape"

2.简单的防止重载

Activity每次横竖屏切换都会重新调用onPause->onStop->onDestory-> onCreate>onStart->onResume,为此涉及到内容和数据的保存和读取,否则转屏之前的内容就会消失了。很多时候为了保存之前的数据,结果让程序变得很繁琐,为此Android提供了在AndroidManifest中设置android:configChanges属性,从而让Activity不延续上述的重建流程。第一次进入Activity会调用onCreate(),横竖屏切换之后就不会去执行OnCreat函数了,而是会去调用onConfigurationChanged()这样就能控制横竖屏的切换了。用户可以在Activity或View的onConfigurationChanged(ConfigurationnewConfig)函数中获取当前横竖屏参数。
在 AndroidManifest.xml中加入:android:configChanges="orientation|keyboardHidden"
在activity中重载onConfigurationChanged事件
@Override
  publicvoid onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
}

注意:
当我们在Android 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,在AndroidManifest.xml中的Activity加入以下属性:
android:configChanges="orientation|keyboardHidden|screenSize"。

3.横竖屏分别布局

横竖屏分别布局的方法是:
在res下新建
layout-land 横屏
layout-port 竖屏
然后把layout中的xml文件分别考到以上目录,修改布局就可以了代码中不做任何更改。
在 AndroidManifest.xml文件中的主Activity中加入
android:configChanges="orientation|keyboardHidden"
然后在主Activity中的onConfigurationChanged加入
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);

if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.main); //布局
tv = (TextView) findViewById(R.id.EditText01);//控件
}

if (config.orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.main); //布局
tv = (TextView) findViewById(R.id.EditText01);//控件
}
}
4.彻底禁止翻转

当然如果要彻底禁止翻转,可以设置android:screenOrientation的属性为nosensor,如此就可以忽略重力感应带来的麻烦了。不过在模拟器上不管用,在真机上是正确的。

这里提一个小知识,Android模拟器中,快捷键"Ctrl+F11/F12"可以实现转屏

5.自适应转换

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

首先:在Mainfest.xml中追加

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

这两个属性。

第二步:取得屏幕的长和宽,进行比较设置横竖屏的变量。

1. Display display = getWindowManager().getDefaultDisplay(); 
2. int width = display.getWidth(); 
3. int height = display.getHeight(); 
4. if (width > height) { 
5.     orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;  //横屏

6. } else { 
7.     orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;  //竖屏

8. } 
第三步:在onConfigurationChanged()函数中追加this.setRequestedOrientation(orientation)就行了

1. public void onConfigurationChanged(Configuration newConfig) { 
2.     super.onConfigurationChanged(newConfig); 
3.     this.setRequestedOrientation(orientation); 
4. } 
但是这样的话你切到别的画面的时候再回到原画面,它就仍然是横的或者是纵的。怎么让它从别的屏幕回来后,又重新横竖屏布局呢?

只要在OnResume()中在设定下就行了。但是这个只支持横竖屏只有一个layout的。横竖屏分别对应layout的还不知道该怎么解决。

1. protected void onResume() { 
2.     orientation = ActivityInfo.SCREEN_ORIENTATION_USER; 
3.     this.setRequestedOrientation(orientation); 
4.     Display display = getWindowManager().getDefaultDisplay(); 
5.     int width = display.getWidth(); 
6.     int height = display.getHeight(); 
7.     if (width > height) { 
8.         orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 
9.     } else { 
10.        orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 
11.    } 
12.    super.onResume(); 
13.} 

猜你喜欢

转载自meohao.iteye.com/blog/1903798