Android中獲取屏幕高度和寬度的方法

在Android開發過程當中,爲了適應各類手機顯示,不少時候須要獲得屏幕高度和寬度,如下提供兩種方法獲取,推薦第二種。java

第一種方法:this

WindowManager wManager=MainActivity.this.getWindowManager();
int screenHeight=wManager.getDefaultDisplay().getHeight();
int screenWidth=wManager.getDefaultDisplay().getWidth();

該方法在高版本的SDK中已經不推薦使用,但若是使用的話也不會產生異常。code

第二種方法:開發

int screenHeight=getResources().getDisplayMetrics().heightPixels;
int screenWidth=getResources().getDisplayMetrics().widthPixels;

獲取屏幕高度和寬度還有其餘方法,在這裏列出兩種主要是供之後參考使用。
get

相關文章
相關標籤/搜索