我怎样才能获得的Andr​​oid Honeycomb系统的屏幕宽度和高度? [英] How can I get android Honeycomb system's screen width and height?

查看:184
本文介绍了我怎样才能获得的Andr​​oid Honeycomb系统的屏幕宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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);

当我运行这个程序的Andr​​oid Honeycomb系统,输出 800和1232 但该设备的屏幕是800_1280,郝我能怎么做呢? 谢谢。

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.

推荐答案

这一块code对我的作品(如果你不介意使用无证方法):

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);

这篇关于我怎样才能获得的Andr​​oid Honeycomb系统的屏幕宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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