android:自动横竖屏切换在各种需求下的处理
2013-04-07 10:51
411 查看
首先,切换横屏的方法,有好几种,这里只说一种,
启动了模拟器之后按Ctrl+F11(有说按F12的,我按是没用)就可以在横竖屏间来回切换了.
默认情况下,在横竖屏切换时,屏幕的布局也会跟着切换,并且会重启当前的activity,
(重载的原理:
If the configuration of the device (as defined by the
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
the activity had been in the foreground or visible to the user, once
instance had generated from
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
of configuration changes you say that you handle there, you will receive a call to your current activity's
any that you do not handle, however, the activity will still be restarted and
)
这个问题有几种应对方法.
1. 假如你想让应用只在一种屏幕状态下进行,那么可以关闭这个横竖屏切换功能,关闭的方法:
在activitymanifest.xml文件中,定义activity时,加上以下的语句.
android:screenOrientation="landscape" //横屏
android:screenOrientation="portrait" //竖屏
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
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//横向
setContentView(R.layout.main_landscape);
findViews();
setListener();
}else{
//竖向
setContentView(R.layout.main);
findViews();
setListener();
}
}
注意重写这个方法需要重新实例化组件和设置监听,否则布局改了,但是监听都失效了
findViews(); , setListener(); 就是完成这两项步骤的.
(3) 最后在onCreate()时加上判断,根据第一次启动时的屏幕状态,使用相应的布局.
[java] view
plaincopy
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.main_landscape);
}else{
setContentView(R.layout.main);
}
4. 允许横竖屏切换,并保留UI的原始状态
(1) 还是先加上横,竖屏自动判断切换的语句.
[java] view
plaincopy
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.main_landscape);
}else{
setContentView(R.layout.main);
}
(2) 重写onRetainNonConfigurationInstance()方法,并用Bundle保存activity的即时状态
底下我的例子代码长了一点...其实取决于你要保存的状态有多少了
[java] view
plaincopy
@Override
public Object onRetainNonConfigurationInstance() {
// TODO Auto-generated method stub
String str = outPut.getText().toString();
Bundle bundle = new Bundle();
bundle.putIntArray("poker", poker);
bundle.putString("outPut",str);
if(str.equals("WOW,选对了哦,真厉害!你是怎么做到的?")){
bundle.putInt("face", R.drawable.qq_suprise);
}else{
bundle.putInt("face", R.drawable.qq_despise);
}
return bundle;
}
(3) 在onCreate()方法中加上if(getLastNonConfigurationInstance() != null)语句
判断是否存储有状态,如果有,就恢复存储的各项状态,没有就为初始化状态.
[java] view
plaincopy
if(getLastNonConfigurationInstance() != null){
Bundle bundle = (Bundle)getLastNonConfigurationInstance();
outPut.setText(bundle.getString("outPut"));
poker = bundle.getIntArray("poker");
imageFace.setImageDrawable(getResources().getDrawable(bundle.getInt("face")));
image_View1.setImageDrawable(getResources().getDrawable(poker[0]));
image_View2.setImageDrawable(getResources().getDrawable(poker[1]));
image_View3.setImageDrawable(getResources().getDrawable(poker[2]));
if(1 == target){
image_View2.setAlpha(100);
image_View3.setAlpha(100);
}else if(2 == target){
image_View1.setAlpha(100);
image_View3.setAlpha(100);
}else if(3 == target){
image_View1.setAlpha(100);
image_View2.setAlpha(100);
}
}else{
random();
}
OK 就能实现切换屏幕,重新配置布局,还能保证UI当前状态的功能咯.
启动了模拟器之后按Ctrl+F11(有说按F12的,我按是没用)就可以在横竖屏间来回切换了.
默认情况下,在横竖屏切换时,屏幕的布局也会跟着切换,并且会重启当前的activity,
(重载的原理:
If the configuration of the device (as defined by the
Resources.Configurationclass) 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:configChangesattribute 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()方法
[java] view
plaincopy
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//横向
setContentView(R.layout.main_landscape);
findViews();
setListener();
}else{
//竖向
setContentView(R.layout.main);
findViews();
setListener();
}
}
注意重写这个方法需要重新实例化组件和设置监听,否则布局改了,但是监听都失效了
findViews(); , setListener(); 就是完成这两项步骤的.
(3) 最后在onCreate()时加上判断,根据第一次启动时的屏幕状态,使用相应的布局.
[java] view
plaincopy
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.main_landscape);
}else{
setContentView(R.layout.main);
}
4. 允许横竖屏切换,并保留UI的原始状态
(1) 还是先加上横,竖屏自动判断切换的语句.
[java] view
plaincopy
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.main_landscape);
}else{
setContentView(R.layout.main);
}
(2) 重写onRetainNonConfigurationInstance()方法,并用Bundle保存activity的即时状态
底下我的例子代码长了一点...其实取决于你要保存的状态有多少了
[java] view
plaincopy
@Override
public Object onRetainNonConfigurationInstance() {
// TODO Auto-generated method stub
String str = outPut.getText().toString();
Bundle bundle = new Bundle();
bundle.putIntArray("poker", poker);
bundle.putString("outPut",str);
if(str.equals("WOW,选对了哦,真厉害!你是怎么做到的?")){
bundle.putInt("face", R.drawable.qq_suprise);
}else{
bundle.putInt("face", R.drawable.qq_despise);
}
return bundle;
}
(3) 在onCreate()方法中加上if(getLastNonConfigurationInstance() != null)语句
判断是否存储有状态,如果有,就恢复存储的各项状态,没有就为初始化状态.
[java] view
plaincopy
if(getLastNonConfigurationInstance() != null){
Bundle bundle = (Bundle)getLastNonConfigurationInstance();
outPut.setText(bundle.getString("outPut"));
poker = bundle.getIntArray("poker");
imageFace.setImageDrawable(getResources().getDrawable(bundle.getInt("face")));
image_View1.setImageDrawable(getResources().getDrawable(poker[0]));
image_View2.setImageDrawable(getResources().getDrawable(poker[1]));
image_View3.setImageDrawable(getResources().getDrawable(poker[2]));
if(1 == target){
image_View2.setAlpha(100);
image_View3.setAlpha(100);
}else if(2 == target){
image_View1.setAlpha(100);
image_View3.setAlpha(100);
}else if(3 == target){
image_View1.setAlpha(100);
image_View2.setAlpha(100);
}
}else{
random();
}
OK 就能实现切换屏幕,重新配置布局,还能保证UI当前状态的功能咯.
相关文章推荐
- android:自动横竖屏切换在各种需求下的处理
- Android 横竖屏切换的处理
- Android开发,相机拍摄并返回时,部分手机出现横竖屏切换的错误处理。
- Android游戏开发9:触屏事件处理中的常见问题及如何禁止横竖屏切换
- android横竖屏切换、键盘推出状态改变的处理
- Android - 数据库自动更新,横竖屏切换,系统控件样式,Fragment添加到Activity,Fragment生命周期,缓存,获取/清除缓存
- android横竖屏切换、键盘推出状态改变的处理
- 【温故知新】之Android中横竖屏切换处理的方式
- android横竖屏切换布局处理及屏幕旋转处理
- Android横竖屏切换处理
- Android 横竖屏切换的处理 (转载)
- Android快速实现横竖屏切换布局自动更改
- android横竖屏切换处理
- Android关于处理运行时变化如横竖屏切换等问题
- 在Android中自动实现横竖屏切换的问题
- android横竖屏切换处理
- Android中配置改变(Configuration)的处理状态(重点横竖屏切换)
- Android中配置改变(Configuration)的处理状态(重点横竖屏切换)
- Android下横竖屏切换的处理
- Android 不自动锁屏 4.0以后横竖屏切换不重新启动