Android中获取屏幕高度和宽度
有时我们需要获取当前屏幕的高度和宽度,只需要在一个Activity的onCreate()方法中写上如下代码即可:
//定义DisplayMetrics 对象
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程序就支持了多种分辨率,那么就可以得到正确的物理尺寸了。
获得屏幕的宽高还有这样两种方法,不过是已经废用的方法,但是还可以使用:
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();
Android中获取屏幕高度和宽度的更多相关文章
- android开发获取屏幕高度和宽度
宽度:getWindowManager().getDefaultDisplay().getWidth(); 高度:getWindowManager().getDefaultDisplay().getH ...
- Android 获取屏幕高度,宽度,状态栏高度
背景介绍: 到目前为止,android已经从1.5发展到目前的3.2,我们在写一个应用的时候,最常用到得就是获取屏幕高度,宽度,以及status bar的高度. 然而android系统变化太快了,从开 ...
- js和jQuery 获取屏幕高度、宽度
js获取屏幕高度,宽带 网页可见区域宽:document.body.clientWidth网页可见区域高:document.body.clientHeight网页可见区域宽:document.body ...
- android 获取屏幕高度和宽度 的方法
我们需要获取Android手机或Pad的屏幕的物理尺寸,以便于界面的设计或是其他功能的实现.下面就介绍讲一讲如何获取屏幕的物理尺寸 下面的代码即可获取屏幕的尺寸. 在一个Activity的onC ...
- 在Android中让Preference的宽度占满整个屏幕的宽度
今天遇到一个问题,需要修改Preference的宽度,让其与屏幕宽度一致.搞了一上午. 终于发现Preference的这个尺寸是在PreferenceFrameLayout中设置的.通过下面这段代码, ...
- 以最省内存的方式把大图片加载到内存及获取Exif信息和获取屏幕高度和宽度的新方法
我们在加载图片时经常会遇到内存溢出的问题,图片太大,我们加载图片时,一般都是用的如下一般方法(加载本地图片): /** * 不作处理,去加载图片的方法,碰到比较大的图片会内存溢出 */ private ...
- jQuery 获取屏幕高度、宽度
做手机Web开发做浏览器兼容用到了,所以在网上找了些汇总下. alert($(window).height()); //浏览器当前窗口可视区域高度alert($(document).height()) ...
- jq获取屏幕高度和宽度(盒子模型)
$(window).width(); //浏览器可视窗口宽度 $(window).height(); //浏览器可视窗口高度 $(document).width();//body的宽度 $(docum ...
- 用javascript获取屏幕高度和宽度等信息
网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...
随机推荐
- [Algorithms] Graph Traversal (BFS and DFS)
Graph is an important data structure and has many important applications. Moreover, grach traversal ...
- mysql中给表添加字段
添加字段: 格式:alter table 表名 add 字段名 字段类型 ; 如:给表stu_info 添加一个字段type,类型为varchar(30) alter table stu_info a ...
- FineReport----单元格元素(数据列、公式、斜线)
一.绑定.插入数据列 数据集字段绑定 二.公式 1.单元 格计算 1.两个单元格(C7,I7)和 2.等于某单元格的值 D7等C7单元格的值 3.C7/8(除) 4.除数被除数为零.空的处理 http ...
- VB 十六进制转汉字的函数
Public Function HexToStr(ByVal strs As String) As String Dim i As Integer, tmp As String, n If Len(s ...
- 9.JavaScript简单计算器的实现
1.难点,怎么获取标签的值,注意点,获取到的值都是string类型,还要转换 var num1 = parseInt(document.getElementById("num1") ...
- poj 1182 食物链 (带关系的并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...
- Shell下syntax error: operand expected (error token is “-”)
在这个监控实时网口速率的脚本中,第21,22行存在错误: #!/bin/bash #Modified by lifei4@datangmobile.cn echo ===DTmobile NetSpe ...
- Using Swift with Cocoa and Objective-C--在同个project中使用Swift和在同个project中
http://www.cocoachina.com/newbie/basic/2014/0605/8688.html watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u ...
- tornado下使用静态文件和文件缓存
静态文件和文件缓存 1.在应用配置 settings 中指定 static_path 选项来提供静态文件服务: 2.在应用配置 settings 中指定 static_url_prefix 选项来 ...
- change the color of a disabled TEdit?
change the color of a disabled TEdit? Author: P. Below Category: VCL {Question:How can I change the ...