在Acitvity的Class當中比較單純
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
若在非Activity的Class下,通常會是在Custom View時,此時若要取得螢幕解析度,可以使用下列程式碼,重點是要使用SystemService當中的Context.WINDOW_SERVICE
WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
若在非Activity的Class下,通常會是在Custom View時,此時若要取得螢幕解析度,可以使用下列程式碼,重點是要使用SystemService當中的Context.WINDOW_SERVICE
WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
width = display.getWidth();
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:resizeable="true"
android:anyDensity="true" />
即可。