手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法
Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法
摘要:本文实例讲述了Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法。分享给大家供大家参考。具体如下:首先定义TextView对...

本文实例讲述了Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法。分享给大家供大家参考。具体如下:

首先定义TextView对象commentText

获取文字的宽高:

TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); textPaint.setTextSize(commentText.getTextSize()); textPaint.setColor(Color.WHITE); FontMetrics fontMetrics = textPaint.getFontMetrics(); float fTop = fontMetrics.top; float fBottom = fontMetrics.bottom; float textHeight = (int)(fBottom - fTop); float textWidth = (int)textPaint.measureText(commentText.getText());

获取手机屏幕上方状态栏高度:

复制代码 代码如下:DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels; //屏幕宽

int height = dm.heightPixels; //屏幕高

Rect frame = new Rect();

getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

int statusBarHeight = frame.top; //状态栏高

int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();

int titleBarHeight = contentTop - statusBarHeight; //标题栏高

获取手机屏幕宽高:

复制代码 代码如下:WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

int width = wm.getDefaultDisplay().getWidth();//屏幕宽度

int height = wm.getDefaultDisplay().getHeight();//屏幕高度

获取textView宽度

TextPaint paint = textView.getPaint(); float len = paint.measureText(string);

获取屏幕尺寸:

DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); double x = Math.pow(dm.widthPixels/dm.xdpi,2); double y = Math.pow(dm.heightPixels/dm.ydpi,2); double screenInches = Math.sqrt(x+y); //屏幕尺寸(英寸)

希望本文所述对大家的Android程序设计有所帮助。

【Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法】相关文章:

Android开发之异步获取并下载网络资源

Android中 动态改变对话框值的方法

Android手机内存中文件的读写方法小结

android获取屏幕像素思路及代码

Android动态添加View的问题解决方法

Android中获取IMEI码的办法

Android手机在开发调试时logcat不显示输出信息的办法

android 动态控制状态栏显示和隐藏的方法实例

android 获取屏幕像素大小的正确方法

Android手机保持屏幕高亮方法

精品推荐
分类导航