主题 : android该如何获取手机屏幕的大小 复制链接 | 浏览器收藏 | 打印
欢迎加入清源的android开发交流群:314230976,加群时请验证:arm,谢谢!
级别: 侠客
UID: 94332
精华: 0
发帖: 72
金钱: 370 两
威望: 74 点
贡献值: 0 点
综合积分: 144 分
注册时间: 2013-07-14
最后登录: 2013-09-25
楼主  发表于: 2013-07-18 19:00

 android该如何获取手机屏幕的大小

android手机的屏幕尺寸问题一直是让开发者感觉很头疼的问题,由于各手机厂商所采用的屏幕尺寸不同,user UI接口呈现及布局自然也各自迥异。所以,在开发android手机应用程序时,除了对底层API的掌握之外,最重要的仍是屏幕分辨率概念的理解。

android可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须清楚地知道手机屏幕的边界,以免缩放之后造成的布局(Layout)变形问题。在android中,只需几行代码就可以取得手机屏幕分辨率,其中的关键则是DisplayMetrics类的应用。

DisplayMetrics类直接继承自Object类,存放在Android.util包下。DisplayMetrics对象记录了一些常用的信息,包含了显示信息、大小、维度和字体等,如下表所示:


复制代码
  1. static int DEFAULT_DENSITY
  2.           The reference density used throughout the system.
  3. float density
  4.           The logical density of the display.
  5. int heightPixels
  6.           The absolute height of the display in pixels.
  7. float scaledDensity
  8.           A scaling factor for fonts displayed on the display.
  9. int widthPixels
  10.           The absolute width of the display in pixels.
  11. float xdpi
  12.           The exact physical pixels per inch of the screen in the X dimension.
  13. float ydpi
  14.           The exact physical pixels per inch of the screen in the Y dimension.




值得一提的是,widthPixels和heightPixels记录了手机屏幕的宽和高,我们使用这两个值便可得到手机屏幕分辨率。注意此处的像素指的是绝对(Absolute)像素,而非相对像素。



下面是获取屏幕分辨率的代码:

复制代码
  1. [java] public class MainActivity extends Activity  
  2. {
  3.     private TextView text=null;
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState)
  6.     {
  7.         super.onCreate(savedInstanceState);
  8.         super.setContentView(R.layout.activity_main);
  9.         this.text=(TextView)super.findViewById(R.id.text);
  10.         DisplayMetrics dm=new DisplayMetrics();
  11.         super.getWindowManager().getDefaultDisplay().getMetrics(dm);
  12.         String strOpt="手机屏幕分辨率为:"+dm.widthPixels+"*"+dm.heightPixels;
  13.         this.text.setText(strOpt);
  14.     }
  15. }
  16. public class MainActivity extends Activity
  17. {
  18.     private TextView text=null;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState)
  21. {
  22.   super.onCreate(savedInstanceState);
  23.   super.setContentView(R.layout.activity_main);
  24.   this.text=(TextView)super.findViewById(R.id.text);
  25.   DisplayMetrics dm=new DisplayMetrics();
  26.   super.getWindowManager().getDefaultDisplay().getMetrics(dm);
  27.   String strOpt="手机屏幕分辨率为:"+dm.widthPixels+"*"+dm.heightPixels;
  28.   this.text.setText(strOpt);
  29. }
  30. }


布局文件非常简单,一个TextView组件即可:

复制代码
  1. [html]  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >
  10.     <TextView
  11.         android:id="@+id/text"
  12.         android:layout_width="wrap_content"
  13.         android:layout_height="wrap_content" />
  14. </RelativeLayout>
  15. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  16.     xmlns:tools="http://schemas.android.com/tools"
  17.     android:layout_width="match_parent"
  18.     android:layout_height="match_parent"
  19.     android:paddingBottom="@dimen/activity_vertical_margin"
  20.     android:paddingLeft="@dimen/activity_horizontal_margin"
  21.     android:paddingRight="@dimen/activity_horizontal_margin"
  22.     android:paddingTop="@dimen/activity_vertical_margin"
  23.     tools:context=".MainActivity" >
  24.     <TextView
  25.         android:id="@+id/text"
  26.         android:layout_width="wrap_content"
  27.         android:layout_height="wrap_content" />
  28. </RelativeLayout>



程序运行效果截图:
欢迎加入android开发交流群,群号是:314230976