gpt4 book ai didi

android - 如何获取android Honeycomb系统的屏幕宽度和高度?

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:39 26 4
gpt4 key购买 nike

我的代码:

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

当我在 android Honeycomb 系统中运行这个程序时,输出是800 和 1232但是设备的屏幕是800_1280,我可以得到吗?谢谢。

最佳答案

这段代码对我有用(如果你不介意使用未记录的方法):

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

关于android - 如何获取android Honeycomb系统的屏幕宽度和高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7144814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com