在我看来,Android 需要一些时间才能从纵向切换到横向(因为最终我会看到我的应用横向").但是 onCreate 在这个开关之前被调用——它扼杀了我的计算.我的猜测正确吗?我尝试将计算从 onCreate() 移动到 onGlobalLayout() 但在执行以下步骤后我仍然得到纵向尺寸:横向启动应用按下睡眠按钮(屏幕变黑)旋转到纵向按下睡眠按钮(屏幕在呈现解锁屏幕时淡出)解锁设备我的应用唤醒时接收纵向尺寸在接下来的 onGlobalLayout() 上,我终于获得了横向尺寸,但为时已晚.我可以完全摆脱纵向吗? 解决方案 我花了几天时间解决同样的问题.正如其他海报所揭示的那样,当从睡眠/锁定屏幕返回时,该应用程序将获得与锁定屏幕相同的纵向模式.真正的发现是它经历了 onResume() 和 onWindowFocusChanged 两次 - 一次用于纵向模式,然后用于横向模式.在执行屏幕初始化之前,我在 onResume() 和 onWindowFocusChanged() 中使用了以下代码进行检查:int gW = view.getMeasuredWidth();int gH = view.getMeasuredHeight();如果 (gW > gH) {//风景!//此时将设置高度//所以在这里重新初始化你的屏幕}它帮助了我.希望这对你有帮助.My android application is designed to be landscape-only. I have put android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden|screenSize"into the manifest.And it really launches in landscape orientation. And that's cool.But!I need to know device screen size to perform some calculations. And I have added following code into onCreate(): Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); // utilize size to calculate something...If I launch the app from portrait orientation, I get (Google Nexus) size.x = 800, size.y = 1205 instead of correct (for landscape) size.x = 1280, size.y = 736.It seems for me that Android needs some time to switch from portrait to landscape (because eventually I see my app "landscaped"). But onCreate is called before this switch -- and it slays my calculations. Is my guess correct?I tried to move calculations from onCreate() to onGlobalLayout() but I still get portrait sizes after following steps:launch app in landscapepress sleep button (screen goes dark)rotate to portrait orientationpress sleep button (screen fades in presenting unlock screen)unlock the devicemy app wakes up receiving portrait orientation sizeon next onGlobalLayout() I get landscape size at last, but it's too late.Can I get rid of portrait orientation completely? 解决方案 I spent days on this same problem. As other posters revealed, when returning from sleep/lock screen, the app is handed the same portrait mode that the lock screen has. The real find was that it goes through onResume() and onWindowFocusChanged twice - once for the portrait mode, then for the landscape mode. I used the following code in both onResume() and onWindowFocusChanged() to check before executing screen initializations:int gW = view.getMeasuredWidth();int gH = view.getMeasuredHeight();if (gW > gH) {// landscape!// the height will be set at this point // so reinitialize your screen here}It helped me. Hope this helps you. 这篇关于android屏幕方向混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 02:16