问题描述:

Since API 17 it is possible to get the actual screen size of a phone with:

if (android.os.Build.VERSION.SDK_INT >= 17){

display.getRealSize(size);

int screen_width = size.x;

screen_height = size.y;

} else {...}

I want to get the real screen size for APIs 8-16. What is the best way to handle the else condition in this case?

网友答案:

The following is my solution for getting the actual screen height on all APIs. When the device has a physical navigation bar, dm.heightPixels returns the actual height. When the device has a software navigation bar, it returns the total height minus the bar. I have only tested on a few devices but this has worked so far.

int navBarHeight = 0;
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
    navBarHeight = resources.getDimensionPixelSize(resourceId);
}

boolean hasPhysicalHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);

if (android.os.Build.VERSION.SDK_INT >= 17){
    display.getRealSize(size);
    int screen_width = size.x;
    screen_height = size.y;
} else if (hasPhysicalHomeKey){
    screen_height = dm.heightPixels;
} else {
    screen_height = dm.heightPixels + navBarHeight;
}
网友答案:

Try this code:

display.getRealSize();

More about visited this link:

http://developer.android.com/reference/android/view/Display.html

or

You look at url:

http://stackoverflow.com/a/1016941/3821823

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
网友答案:
else{
   display.getWidth()
   display.getHeight()
}

edit: alternative solution

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.widthPixels
metrics.heightPixels

from the docs:

The absolute width of the display in pixels.

if even after those method the device insist in not giving it's real size, I don't believe there will be much you can do without editing the build.prop file on the device.

相关阅读:
Top