现在,吵吵闹闹的要生产Android手机的厂商越来越多,从开发者的角度来说,不能不说这是一件好事,毕竟写出来的程序可以顺利的跑在更多的设备上,但是参差不及的硬件选择也给我们带来了很大的麻烦,最直接的就是不同的手机屏幕的尺寸可能不一样,又不能为每种尺寸都设计一套布局,所以就有可能需要在程序运行时来通过屏幕的大小来动态的调整一些布局相关的参数。那如何获得屏幕的大小呢?

在android.app.Activity中提供这样的方法:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;

其中,screenWidth就是屏幕的宽度,screenHeight就是屏幕的高度。

如果将手机从肖像模式(竖着拿)变成风景模式(横着拿),这两个值也会随之改变,所以,也许我们不用这种方法,也可以推算出手机的手持方式,从而来改变一些布局参数。

Tags: ,

46 Responses to “如何在运行时获得屏幕尺寸?”

  1. Hello! kcfafkd interesting kcfafkd site!

  2. Very nice site! cheap viagra

  3. Very nice site! [url=http://aixopey.com/qqatat/2.html]cheap cialis[/url]

  4. Very nice site! cheap cialis http://aixopey.com/qqatat/4.html

  5. Very nice site!

  6. Hello!
    buy cialis ,

  7. Hello! feceged interesting feceged site!

  8. Very nice site! cheap viagra

  9. Very nice site! [url=http://opeyixa.com/rvqatx/2.html]cheap cialis[/url]

  10. Very nice site! cheap cialis http://opeyixa.com/rvqatx/4.html

  11. Very nice site!

  12. http://www.roiding.com is very interesting. I enjoyed your blog a lot. Thank you.

  13. Hello!
    cialis ,

  14. Hello!
    buy viagra ,

  15. Hello!
    viagra online ,

  16. Hello!
    viagra ,

  17. Hello!
    cialis ,

  18. Hello!
    viagra online ,

  19. Hello!
    viagra ,

  20. Tucked away in our timberland for you subconscious is an idyllic vision. We see ourselves on a long trip that timberland 6 inch spans the continent. We are traveling by train. Out timberland hiking boots windows, we drink in the passing scene of cars on nearby highways, of children timberland shoe company waving at a crossing, of cattle grazing on a distant timberland boots hillside, of smoke pouring from a power plant, of row upon row of corn and wheat, of flatlands and timberland wheat shoes valleys, of mountains and rolling classic 3 eye timberland boat hillsides, of city skylines and village halls.But uppermost in our black timberland boots minds is the final destination. On a certain cheap timberland boots day at a certain hour, we will pull into the station. Bands will be playing and flags discount timberland boots waving. Once we get there, so many wonderful timberland winter boots dreams will come true and the pieces of our lives will fit together like a completed jigsaw puzzle. How restlessly we pace the aisles, womens timberland boot the minutes for loitering –waiting, waiting, waiting for the station.But uppermost in our timberland shoes store minds is the final destination. On a certain day at a certain hour, we will pull into the station. Bands will be playing and timberland eye boat flags waving. http://www.timberland6inch.com/

  21. Hello!
    viagra ,

  22. Hello!
    viagra online ,

  23. http://www.roiding.com is great! No faing payday loans offer small amount of money without taking any assets from the borrowers The service is also free from lengthy formalities and paperwork

  24. Hello!
    cheap cialis ,

  25. Hello!
    cheap viagra ,

  26. Hello!
    buy viagra ,

  27. Hello!
    viagra ,

  28. Hello!
    cialis ,

  29. Hello!
    cialis ,

  30. Hello!
    cheap viagra ,

  31. Hello!
    cialis ,

  32. Hello!
    cialis ,

  33. Hello!
    viagra ,

  34. so much of it rings true..

  35. Hello!
    cheap viagra ,

  36. Hello!
    cialis ,

  37. I agree wholeheartedly.

  38. Hello!
    cheap cialis ,

  39. Hello!
    viagra ,

  40. Hello!
    cialis ,

  41. Hello!
    viagra ,

  42. Hello!
    viagra ,

  43. Hello!
    buy cialis ,

  44. Hello!
    viagra online ,

  45. Hello!
    cheap viagra ,

  46. Hello!
    viagra ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>