如何在Android 2.3.3中使用onConfigurationChanged()和newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE [英] How to use onConfigurationChanged() and newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE in android 2.3.3

查看:266
本文介绍了如何在Android 2.3.3中使用onConfigurationChanged()和newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用onConfigurationChanged().在这种情况下,当我从LandScape更改为Portrait时,它将调用if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)并从LandScape转换为Portrait.但是,当我从Portrait更改为Land-Scape时,它并没有更改为LandScape,因为它正在调用if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE),所以它也没有从LandScape更改为Portrait. 请帮忙.

I am using onConfigurationChanged(). In that, when I am changing from LandScape to Portrait, it is calling if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) and shifting to Portrait from LandScape. But when I am changing from Portrait to Land-Scape, it is not changing to LandScape because it is calling if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) so, it is not changing from LandScape to Portrait. Please help.

public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        //int orientation = this.getResources().getConfiguration().orientation;

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Log.d("Entered to change as Portrait ","PPPPPPPPPPPPPPPPP");
            setContentView(R.layout.settings);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.d("Entered to change as LandScape ","LLLLLLLLLLLLLLLLLLLL");
            setContentView(R.layout.settings);
        }

    }

推荐答案

只需将以下代码写入onConfigurationChanged方法并进行测试

Just write the below code into onConfigurationChanged method and test

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){

    Log.e("On Config Change","LANDSCAPE");
}else{

    Log.e("On Config Change","PORTRAIT");
}

,然后将android:configChanges="keyboardHidden|orientation"写入到您的清单文件中

and write the android:configChanges="keyboardHidden|orientation" into your manifiest file like this

<activity android:name="TestActivity"
           android:configChanges="keyboardHidden|orientation">

它在我这边工作,希望对您有帮助.

it's working at my side, i hope it helps you.

如果您在平板电脑上,也将|screenSize添加到android:configChanges

If you're on tablet also add |screenSize to android:configChanges

这篇关于如何在Android 2.3.3中使用onConfigurationChanged()和newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆