1.

WindowManager wm = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();

2.

WindowManager wm = this.getWindowManager();

     int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();

3、 在一个Activity的onCreate方法中,写入如下代码:

DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; // 屏幕宽度(像素)
int height = metric.heightPixels; // 屏幕高度(像素)
float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
int densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)

但是,需要注意的是,在一个低密度的小屏手机上,仅靠上面的代码是不能获取正确的尺寸的。比如说,一部240x320像素的低密度手机,如果运行上述代码,获取到的屏幕尺寸是320x427。因此,研究之后发现,若没有设定多分辨率支持的话,Android系统会将240x320的低密度(120)尺寸转换为中等密度(160)对应的尺寸,这样的话就大大影响了程序的编码。所以,需要在工程的AndroidManifest.xml文件中,加入supports-screens节点,具体的内容如下:

<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:resizeable="true"
android:anyDensity="true" />

这样的话,当前的Android程序就支持了多种分辨率,那么就可以得到正确的物理尺寸了。

android获取屏幕宽度和高度的更多相关文章

  1. Android获取屏幕宽度、高度的4种方法

    记录学习之用,有相同的问题可以参考 方法一: WindowManager wm = (WindowManager) this .getSystemService(Context.WINDOW_SERV ...

  2. 【转】android获取屏幕宽度和高度

    原文网址:http://www.cnblogs.com/howlaa/p/4123186.html 1. WindowManager wm = (WindowManager) getContext() ...

  3. android 获取屏幕宽度和高度

    // 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480p ...

  4. Android获取屏幕宽度高度

    方法一: WindowManager wm = (WindowManager) this .getSystemService(Context.WINDOW_SERVICE); int width = ...

  5. android Xml生成一条细线,以及获取屏幕宽度和高度

    <View android:layout_width="match_parent" android:layout_height="2dip" androi ...

  6. Android获取屏幕宽度的4种方法

    方法一: WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); int width = w ...

  7. Js获取屏幕宽度、高度

    document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...

  8. Android获取屏幕高度、标题高度、状态栏高度详解

    Android获取屏幕高度的方法主要由view提供 通过View提供的方法获取高度方式有两种: 1, 当前显示的view中直接获取当前view高宽2,通过Activity的getWindow().fi ...

  9. 微信小程序如何获取屏幕宽度

    微信小程序如何获取屏幕宽度 方法1: imageLoad: function () { this.setData({ imageWidth: wx.getSystemInfoSync().window ...

随机推荐

  1. Linux服务器---流量监控ntop

    Ntop Ntop 是一款类似于sniffer的流量监控工具,它显示出的流量信息比mrtg更加详细. 1 .安装一些依赖软件 [root@localhost bandwidthd]#  yum ins ...

  2. 转:【专题五】TCP编程

    前言 前面专题的例子都是基于应用层上的HTTP协议的介绍, 现在本专题来介绍下传输层协议——TCP协议,主要介绍下TCP协议的工作过程和基于TCP协议的一个简单的通信程序,下面就开始本专题的正文了. ...

  3. PyCharm 2017.2.3 版本在2017年9月7日发布,支持 Docker Compose

    PyCharm是由JetBrains打造的一款Python IDE.PyCharm具备用于一般IDE的功能,比如, 调试.语法高亮.Project管理.代码跳转.智能提示.自动完成.单元测试.版本控制 ...

  4. SQL Server 2008 R2 常用系统函数学习

    /******************************************* * 聚合函数 *******************************************/ SEL ...

  5. SQL Server char,varchar,nchar,nvarchar区别

    SQL Server char,varchar,nchar,nvarchar区别 (1)       定义: char:    固定长度,存储ANSI字符,不足的补英文半角空格. nchar:   固 ...

  6. SRTP参数及数据包处理过程(转)

    源: SRTP参数及数据包处理过程

  7. 软件工程 #02# Entity Relationship Diagram VS. 用 UML 中的类图表示 E-R 图

    不同的老师叫我们画 E-R 图居然是不一样的,于是我仔细研究了一番.. 通常所说的 E-R 图(外文全称 Entity Relationship Diagram,简称 ERD)长这个样子: 而有时候它 ...

  8. Pytorch的torch.cat实例

    import torch 通过 help((torch.cat)) 可以查看 cat 的用法 cat(seq,dim,out=None) 其中 seq表示要连接的两个序列,以元组的形式给出,例如:se ...

  9. Prometheus监控学习笔记之全面学习Prometheus

    0x00 概述 Prometheus是继Kubernetes后第2个正式加入CNCF基金会的项目,容器和云原生领域事实的监控标准解决方案.在这次分享将从Prometheus的基础说起,学习和了解Pro ...

  10. GO语言学习笔记之Linux环境下安装GO语言

    0x00 安装环境和GO版本 本篇是源码安装,非使用包管理工具安装. # Centos 7.4 # GO v1.11.2 0x01 下载GO安装包 # wget https://dl.google.c ...