1. 程式人生 > >Android獲得螢幕寬度和高度

Android獲得螢幕寬度和高度

//直接在子元件中 1、WindowManager wm = (WindowManager) getContext()                     .getSystemService(Context.WINDOW_SERVICE);        //目前版本已經不建議使用了,建議使用下面的方式      int width = wm.getDefaultDisplay().getWidth();      int height = wm.getDefaultDisplay().getHeight();

獲取文字的寬度和高度

//1. 返回包圍串的最小的一個rect區域 Paint p = new Paint(); Rect rect = new Rect(); p.getTextBounds(str,0,1,rect);

w = rect.getWidth(); h = rect.getHeight();

//2. 直接獲得文字的寬度 String displayText = “Hello World!”;   float textWidth = p.measureText(displayText);