Android Studio——Android获取屏幕宽度的4种方法
16lz
2021-01-23
原文链接:http://www.androidchina.net/2022.html
方法一:
1 2 3 | WindowManagerwm=(WindowManager) this .getSystemService(Context.WINDOW_SERVICE); int width=wm.getDefaultDisplay().getWidth(); int height=wm.getDefaultDisplay().getHeight();</div> |
方法二:
1 2 3 | WindowManagerwm1= this .getWindowManager(); int width1=wm1.getDefaultDisplay().getWidth(); int height1=wm1.getDefaultDisplay().getHeight(); |
方法一与方法二获取屏幕宽度的方法类似,只是获取WindowManager 对象时的途径不同。
方法三:
1 2 3 4 5 | WindowManagermanager= this .getWindowManager(); DisplayMetricsoutMetrics= new DisplayMetrics(); manager.getDefaultDisplay().getMetrics(outMetrics); int width2=outMetrics.widthPixels; int height2=outMetrics.heightPixels; |
方法四:
1 2 3 4 5 | Resourcesresources= this .getResources(); DisplayMetricsdm=resources.getDisplayMetrics(); float density1=dm.density; int width3=dm.widthPixels; int height3=dm.heightPixels; |
方法三与方法四类似。
更多相关文章
- Android Studio引入FFmpeg的方法
- Android TextUtils类常用方法
- Android简单实现启动画面的方法
- Android 判断Root的方法
- android 获取锁屏,解锁的方法
- android中的activity里获得context方法
- android paint设置字体 中文字体 楷体 和自动换行方法(zhuan)