How can I get android Honeycomb system's screen width and height?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 07:44:02

问题


my code:

Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);

System.out.println("screen width:"+displayMetrics.widthPixels);
System.out.println("screen heigth:"+displayMetrics.heightPixels);

when I run this program in android Honeycomb system,the output is 800 and 1232 but the device's screen is 800_1280,hao can I get it? thanks.


回答1:


This piece of code works for me (if you don't mind to use undocumented methods):

Display d = getWindowManager().getDefaultDisplay();
int width, height;

Method mGetRawH;
Method mGetRawW;
try {
    mGetRawH = Display.class.getMethod("getRawWidth");
    mGetRawW = Display.class.getMethod("getRawHeight");
    width = (Integer) mGetRawW.invoke(d);
    height = (Integer) mGetRawH.invoke(d);
} catch (NoSuchMethodException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}

//You should care about orientation here
int o = getResources().getConfiguration().orientation;
if (o == Configuration.ORIENTATION_PORTRAIT) {
    if (width > height) {
        int tmp = width;
        width = height;
        height = tmp;
    }
} else if (o == Configuration.ORIENTATION_LANDSCAPE) {
    if (width < height) {
        int tmp = width;
        width = height;
        height = tmp;
    }
}

Log.d("d", "w=" + width + " h=" + height);



回答2:


Actually device full screen width=800 and height=1280 (including status bar, title bar). If you run your code to get display size, the system will not include status bar height and title bar height, so you will get height as 1232 (1280-(status bar height + title bar height)).




回答3:


1280 x 752 - landscape 800 x 1232 - portrait

This 48 px are for the Andriod default notification bar...



来源:https://stackoverflow.com/questions/7144814/how-can-i-get-android-honeycomb-systems-screen-width-and-height

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