Android:屏幕密度未正确报告
在开发 Android 应用程序的过程中,我注意到以下代码行没有报告我的 Samsung Nexus S 的正确屏幕尺寸:
width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;
宽度返回为 320,而高度设置为 533。来自 这篇文章 我知道这是一个与未考虑设备像素密度有关的问题。为了纠正这个问题,我编写了下面的代码:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int density = dm.densityDpi;
int width = (density / 160) * dm.widthPixels;
int height = (density / 160) * dm.heightPixels;
当我烘烤变量 width
和 height
时,返回的尺寸为 320 宽和 533 高,这意味着没有任何变化。此外,dm.密度Dpi
返回 160,而不是 235,而 Samsung Nexus S 应该返回 235。
我读过的一些帖子似乎表明问题可能出在 AndroidManifest.xml 中。我已经将以下内容添加到我的 XML 文件中,试图解决此问题:
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="10"
/>
<supports-screens
android:anyDensity="false"
android:resizeable="false"
android:largeScreens="true"
android:normalScreens="true"
/>
但是,唉,这也不起作用。那么,有谁知道如何让手机正确返回其密度?
In the course of developing my Android application, I noticed that the following lines of code were not reporting the correct screen size for my Samsung Nexus S:
width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;
Width was returned as 320, while height got set to 533. From this post I know that this is a problem relating to the pixel density of the device not being taken into account. To correct for this, I wrote up the code below:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int density = dm.densityDpi;
int width = (density / 160) * dm.widthPixels;
int height = (density / 160) * dm.heightPixels;
When I toast the variables width
and height
, the returned dimensions are 320 wide and 533 tall, meaning that nothing changed. Furthermore, dm.densityDpi
returns 160, and not 235, as it should for the Samsung Nexus S.
Some posts that I have read, seem to suggest that the problem might lie within the AndroidManifest.xml. I have already added the following to my XML file in an attempt to solve this issue:
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="10"
/>
<supports-screens
android:anyDensity="false"
android:resizeable="false"
android:largeScreens="true"
android:normalScreens="true"
/>
But, alas, this has not worked either. So, does anyone know how I can get the phone to correctly return its density?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个“问题”与密度无关,这是因为您的应用程序在兼容模式下运行。您的清单必须报告anyDensity=true。
This "problem" has nothing to do with density, it's because your application runs in compatibility mode. Your manifest must report anyDensity=true.