如何正确的得到屏幕大小信息?
2013-02-01 16:11
417 查看
http://stackoverflow.com/questions/1016896/android-how-to-get-screen-dimensions
I created some custom elements and I want to programatically place them to the upper right corner (n pixels from the top edge and m pixels from the right edge) therefore I need to get the screen width and screen height and then set position:
Does anyone know how to get screenWidth and screenHeight in the main Activity?
Thanks
If you want the the display dimensions in pixels you can use
If you're not in an
Before
For the use case you're describing however a margin/padding in the layout seems more appropriate.
I created some custom elements and I want to programatically place them to the upper right corner (n pixels from the top edge and m pixels from the right edge) therefore I need to get the screen width and screen height and then set position:
int px = screenWidth - m;int py = screenWidth - n;
Does anyone know how to get screenWidth and screenHeight in the main Activity?
Thanks
If you want the the display dimensions in pixels you can use
getSize:
Display display = getWindowManager().getDefaultDisplay();Point size =newPoint(); display.getSize(size);int width = size.x;int height = size.y;
If you're not in an
Activityyou can get the default
Displayvia
WINDOW_SERVICE:
WindowManager wm =(WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);Display display = wm.getDefaultDisplay();
Before
getSizewas introduced (in API level 13), you could use the
getWidthand
getHeightmethods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();int width = display.getWidth();// deprecatedint height = display.getHeight();// deprecated
For the use case you're describing however a margin/padding in the layout seems more appropriate.
相关文章推荐
- 转载:如何截取屏幕大小信息
- 如何得到屏幕和可用显示区域的大小尺寸及运用分辨率无关的编程
- (winform cookie)如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- 得到屏幕大小的两种方法
- 如何正确得到某个元素的位置
- 安卓手机开发,手机上应用百度地图得到了经纬度,如何将这些经纬度信息上传到远程服务器中数据库里?
- 如何Linux下得到CPU、内存及PCI信息
- 如何让图片自适应手机等移动设备屏幕大小
- 无法正确显示物理内存大小该如何处理
- 如何用js得到当前页面的url信息方法
- 对话框随屏幕分辨率不同而变化时,TAB控件上的控件如何动态调整大小?
- android系统如何自适应屏幕大小
- Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
- Unity3D之如何创建正确的像素比在屏幕上
- 【转】如何得到Windows操作系统版本信息(包括2003 R2、Vista和Windows 2008 Server)
- (论坛答疑点滴)如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
- Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
- 如何运用CSS在HTML中让图片自动适应屏幕大小的问题
- 如何得到QML中的TextArea的slider的位置信息
- 关于J2EE项目中三层架构如何在开发中得到正确的实施