如何在android 2.3.3中使用onConfigurationChanged()和newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE

2023-11-26

我在用onConfigurationChanged()。在这一点上,当我从风景变为肖像时,它在召唤if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)并从风景转向肖像。但是当我从纵向更改为横向时,它并没有更改为横向,因为它正在调用if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)因此,它并没有从横向变为纵向。 请帮忙。

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方法及试验

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

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

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

并写下android:configChanges="keyboardHidden|orientation"像这样进入你的清单文件

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

它在我身边工作,我希望它对你有帮助。

如果您使用的是平板电脑,还可以添加|screenSize to android:configChanges

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在android 2.3.3中使用onConfigurationChanged()和newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE 的相关文章

随机推荐