Wrong display width and height in Android

£可爱£侵袭症+ 提交于 2019-12-21 04:46:14

问题


I have a screen size of 600 (width) x 1024 (height). I get current width 600 but incorrectly get height 976 (without rotated screen). I get current width 1024 but wrong get height 552 (with rotated screen).

 int rowPixelWidth = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
                int rowWidth =  (int)Math.floor(rowPixelWidth / this.getResources().getDisplayMetrics().density);
                int rowPixelheight = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
                int rowheight =  (int)Math.floor(rowPixelheight / this.getResources().getDisplayMetrics().density);
                Log.d("rowWidth","rowWidth"+rowWidth);
                Log.d("rowheight","rowheight"+rowheight);
-------------------------------------------------------------
 <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="8" />
    <supports-screens android:anyDensity="true"/>
-------------------------------------------------------------

What's wrong with this code?

Here are some numbers for typical screen widths:

  1. 320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
  2. 480dp: a tweener tablet like the Streak (480x800 mdpi).
  3. 600dp: a 7” tablet (600x1024 mdpi).
  4. 720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).

I get current width but not get current height for all device (without (320x480 screen).

I try code from Get screen dimensions in pixels but same occur problem.


回答1:


I believe the getDefaultDisplay only gives you the window where the app is going to be displayed, it does not account for other things like notification bar on top or android buttons (home, back...) at the bottom or things like that.

That's probably why you see a difference of 48 pixels in the vertical size in both cases.

MORE INFO

Your code seems to be right and similar to what's there. You might also want to use something like in this answer to use the right API for the right version of Android.

But in any case, the size you will get will not include the navigation bar with the soft Android buttons when they are present.

When you run Android 4.1 on a HVGA screen, there is no navigation bar or combined bar (screen is too small for that), so you get the resolution for the whole screen.

If you run the same version on a larger screen, you will see the navigation bar, the display size available for your app is smaller and you will only get the resolution for that space.

Please check the definitions of navigation bar and others here if you are not clear.




回答2:


Your screen height with above code doesn't includes the android buttons (home, back, recent apps) at the bottom with a width of 48px. As a result of which you are getting screen height as 1024-48 = 976.

Above function gives the area in which app can be seen. It is possible to make your app as full screen app, which will hide the notification bar at top, but it still wont hide android buttons bar (home, back, recent apps) at the bottom. So the maximum screen space you can utilize is 1024-48=976 px.

However in some phones android buttons are not a part of screen, in those cases you can use whole 1024 px of screen.



来源:https://stackoverflow.com/questions/16140411/wrong-display-width-and-height-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!