问题
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 landscape
- press sleep button (screen goes dark)
- rotate to portrait orientation
- press sleep button (screen fades in presenting unlock screen)
- unlock the device
- my app wakes up receiving portrait orientation size
- on next onGlobalLayout() I get landscape size at last, but it's too late.
Can I get rid of portrait orientation completely?
回答1:
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.
回答2:
no you cannot.
If you want to get view sizes you can override the onLayout http://developer.android.com/reference/android/view/View.html#onLayout(boolean, int, int, int, int)
来源:https://stackoverflow.com/questions/16775871/android-screen-orientation-mixing-up