android:自动横竖屏切换在各种需求下的处理

来源:互联网 发布:微信做淘宝客怎么拉人 编辑:程序博客网 时间:2024/04/27 22:17

首先,切换横屏的方法,有好几种,这里只说一种,

启动了模拟器之后按Ctrl+F11(有说按F12的,我按是没用)就可以在横竖屏间来回切换了.

默认情况下,在横竖屏切换时,屏幕的布局也会跟着切换,并且会重启当前的activity,

(重载的原理:

If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.

 

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed , going through the normal activity lifecycle process of onPause() onStop() , and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated fromonSaveInstanceState(Bundle) .

 

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity'sonConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted andonConfigurationChanged(Configuration) will not be called.

)

 

这个问题有几种应对方法.


1. 假如你想让应用只在一种屏幕状态下进行,那么可以关闭这个横竖屏切换功能,关闭的方法:

在activitymanifest.xml文件中,定义activity时,加上以下的语句.

 

android:screenOrientation="landscape"    //横屏

android:screenOrientation="portrait"    //竖屏

 

 

public static final int screenOrientation

Since: API Level 1

Specify the orientation an activity should be run in. If not specified, it will run in the current preferred orientation of the screen.


备注:虽然看上去UI状态并没有改变,但是Activity还是destroyed,重新onCreate了

 

 

2. 假如你不想在横,竖屏状态后重载onCreate()方法,则可以在activitymanifest.xml中添加下面的语句

android:configChanges="keyboardHidden|orientation"

 

3. 如果你允许横竖屏切换,但是需要改变布局文件的话,可以采取以下方法:(此时原activity的一些组件状态将不保留)

 

(1)先更改设置,在activitymanifest.xml文件中,定义activity时,加上以下的语句.

android:configChanges="keyboardHidden|orientation"

 

并且加上如下权限:

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>

 

(2)重写onConfigurationChanged()方法

 

[java] view plaincopy
  1. @Override  
  2.     public void onConfigurationChanged(Configuration newConfig) {  
  3.         // TODO Auto-generated method stub  
  4.         super.onConfigurationChanged(newConfig);  
  5.         if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){    
  6.             //横向    
  7.             setContentView(R.layout.main_landscape);    
  8.             findViews();  
  9.             setListener();  
  10.         }else{    
  11.             //竖向    
  12.             setContentView(R.layout.main);    
  13.             findViews();  
  14.             setListener();  
  15.               
  16.         }    
  17.     }  

 

注意重写这个方法需要重新实例化组件和设置监听,否则布局改了,但是监听都失效了

findViews(); , setListener(); 就是完成这两项步骤的.

 

(3) 最后在onCreate()时加上判断,根据第一次启动时的屏幕状态,使用相应的布局.

 

[java] view plaincopy
  1. if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){  
  2.             setContentView(R.layout.main_landscape);  
  3.         }else{  
  4.             setContentView(R.layout.main);  
  5.         }  

 

 

4. 允许横竖屏切换,并保留UI的原始状态

 

(1) 还是先加上横,竖屏自动判断切换的语句.

 

[java] view plaincopy
  1. if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){  
  2.         setContentView(R.layout.main_landscape);  
  3.        }else{  
  4.         setContentView(R.layout.main);  
  5.        }  

 

 

(2) 重写onRetainNonConfigurationInstance()方法,并用Bundle保存activity的即时状态

底下我的例子代码长了一点...其实取决于你要保存的状态有多少了

 

 

[java] view plaincopy
  1. @Override  
  2.     public Object onRetainNonConfigurationInstance() {  
  3.         // TODO Auto-generated method stub  
  4.         String str = outPut.getText().toString();  
  5.           
  6.         Bundle bundle = new Bundle();  
  7.         bundle.putIntArray("poker", poker);  
  8.         bundle.putString("outPut",str);  
  9.           
  10.         if(str.equals("WOW,选对了哦,真厉害!你是怎么做到的?")){  
  11.             bundle.putInt("face", R.drawable.qq_suprise);  
  12.         }else{  
  13.             bundle.putInt("face", R.drawable.qq_despise);  
  14.         }  
  15.           
  16.           
  17.         return bundle;  
  18.     }  

 

 

(3) 在onCreate()方法中加上if(getLastNonConfigurationInstance() != null)语句

判断是否存储有状态,如果有,就恢复存储的各项状态,没有就为初始化状态.

 

[java] view plaincopy
  1.    if(getLastNonConfigurationInstance() != null){  
  2.         Bundle bundle = (Bundle)getLastNonConfigurationInstance();  
  3.         outPut.setText(bundle.getString("outPut"));  
  4.         poker = bundle.getIntArray("poker");  
  5.         imageFace.setImageDrawable(getResources().getDrawable(bundle.getInt("face")));  
  6.           
  7.         image_View1.setImageDrawable(getResources().getDrawable(poker[0]));  
  8. image_View2.setImageDrawable(getResources().getDrawable(poker[1]));  
  9. image_View3.setImageDrawable(getResources().getDrawable(poker[2]));  
  10.   
  11. if(1 == target){  
  12.     image_View2.setAlpha(100);  
  13.     image_View3.setAlpha(100);  
  14. }else if(2 == target){  
  15.     image_View1.setAlpha(100);  
  16.     image_View3.setAlpha(100);  
  17. }else if(3 == target){  
  18.     image_View1.setAlpha(100);  
  19.     image_View2.setAlpha(100);  
  20. }  
  21.   
  22.      }else{  
  23.        
  24.         random();  
  25.      }  

 

 

OK 就能实现切换屏幕,重新配置布局,还能保证UI当前状态的功能咯.