手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >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定制RadioButton样式三种实现方法

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

Android手机保持屏幕高亮方法

Android不读入内存获取图像宽高信息的方法

android全屏去掉title栏的多种实现方法

Android检测Cursor泄漏的原理以及使用方法

精品推荐
分类导航