本文實例講述了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());
獲取手機屏幕上方狀態欄高度:
獲取手機屏幕寬高:
獲取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程序設計有所幫助。
新聞熱點
疑難解答
圖片精選