开发者

Android: Screen Density is Not Being Reported Correctly

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 kno开发者_Go百科w 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?


This "problem" has nothing to do with density, it's because your application runs in compatibility mode. Your manifest must report anyDensity=true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜