简体   繁体   English

如何在摩托罗拉里程碑中获得屏幕宽度和高度,android

[英]How to get screen width and height in motorola milestone, android

I have used the followng ways to get screen width and height.我使用了以下方法来获取屏幕宽度和高度。 When I run my application in motorola I got width= 320pixel and height = 533 pixel.当我在摩托罗拉运行我的应用程序时,我得到宽度 = 320 像素和高度 = 533 像素。 But size of motorla milestone is 480 x 854 pixels.但是 motorla 里程碑的大小是 480 x 854 像素。 How to get height as 854 and width as 480 see http://www.gsmarena.com/motorola_milestone-3001.php如何获得高度为 854 和宽度为 480 参见http://www.gsmarena.com/motorola_milestone-3001.php

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

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

int heightOfScreen = metrics.heightPixels;
int widthOfScreen = metrics.widthPixels;
System.out.println("...w1..."+width+"...h1..."+height+"....w2...."+widthOfScreen+"...h2..."+heightOfScreen);

Output w1=320...h1=533...w2=320...h2=533 Output w1=320...h1=533...w2=320...h2=533

I have not tried this, but from what you have tried so far I'm willing to bet this will work:我还没有尝试过,但从你到目前为止所尝试的情况来看,我敢打赌这会奏效:

DisplayMetrics metrics = getResources().getDisplayMetrics();
float width = metrics.widthPixels * metrics.xdpi;
float height = metrics.heightPixels * metrics.ydpi;

Edit : you can also try Display.getSize .编辑:您也可以尝试Display.getSize

Second edit : scratch that.第二次编辑:从头开始。 My first solution is incorrect and my second solution is API Level 13+ only.我的第一个解决方案不正确,我的第二个解决方案是 API 级别 13+。 However, I have tried both your methods and they worked correctly on a Huawei Ideos X5 (Android 2.2) and a Nexus S (Android 2.3.4).但是,我已经尝试了您的两种方法,并且它们在华为 Ideos X5 (Android 2.2) 和Nexus S (Android 2.3.4) 上都能正常工作。 It might just be Motorola's fault here.这可能只是摩托罗拉的错。 Are there any official updates available for your phone's OS?您的手机操作系统是否有可用的官方更新?

I have tried with the following approach.我尝试过以下方法。 It worked for me.它对我有用。

WindowManager windowManager = (WindowManager) activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
int height = windowManager.getDefaultDisplay().getHeight();
int width = windowManager.getDefaultDisplay().getWidth();

Try following尝试关注

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    Log.i(TAG, "Width " + display.getWidth() + "\t\t Height :" + display.getHeight());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM