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

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

启动了模拟器之后按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 from onSaveInstanceState(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's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(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()方法

 

 

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

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

 

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

 

 

 

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

 

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

 

 

 

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

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

 

 

 

 

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

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

 

 

 

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值