Android 获取屏幕宽高
时间: 2023-12-19 21:31:32 浏览: 111
获取屏幕宽高
以下是在Android中获取屏幕宽高的方法:
1.使用DisplayMetrics类获取屏幕宽高
```java
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
```
2.使用WindowManager类获取屏幕宽高
```java
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
```
3.使用Resources类获取屏幕宽高
```java
Resources resources = getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
```
阅读全文