Android中的显示宽度和高度错误 [英] Wrong display width and height in Android

查看:54
本文介绍了Android中的显示宽度和高度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的屏幕尺寸为600(宽)x 1024(高).我得到了当前宽度600,但错误地得到了高度976(没有旋转屏幕).我得到当前宽度1024,但错误得到高度552(屏幕旋转).

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"/>
-------------------------------------------------------------

此代码有什么问题?

以下是一些典型屏幕宽度的数字:

Here are some numbers for typical screen widths:

  1. 320dp:典型的电话屏幕(240x320 ldpi,320x480 mdpi,480x800 hdpi等).
  2. 480dp:像Streak(480x800 mdpi)这样的补间图形输入板.
  3. 600dp:7英寸平板电脑(600x1024 mdpi).
  4. 720dp:10英寸平板电脑(720x1280 mdpi,800x1280 mdpi等).

我获得了所有设备的当前宽度,但没有获得当前的高度(没有(320x480屏幕).

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.

推荐答案

我相信 getDefaultDisplay 仅为您提供了将要显示应用程序的窗口,它并不能解决其他类似问题顶部的通知栏或底部的android按钮(主页,后方...).

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.

这可能就是为什么两种情况下垂直尺寸相差48像素的原因.

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

更多信息

您的代码似乎是正确的,并且与类似.您可能还想在此答案中使用类似的内容,以便为正确的Android版本使用正确的API.

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.

但是在任何情况下,显示的尺寸都不会包括带有软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.

在HVGA屏幕上运行Android 4.1时,没有导航栏或组合栏(屏幕太小了),因此您可以获得整个屏幕的分辨率.

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.

这篇关于Android中的显示宽度和高度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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