Android 获取屏幕宽高值

时间:2016年3月7日16:14:52

note:单位为像素。

三个方法中都是根据Display来进行测量。

  1. //方法一:
  2. WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
  3. Display display = wm.getDefaultDisplay();
  4. int width = display.getWidth();
  5. int height = display.getHeight();
  6. System.out.println("width = " + width + " , height = " + height);
  1. //方法二:也可以获取到屏幕的密度density
  2. DisplayMetrics metrics = new DisplayMetrics();
  3. ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
  4. int widthPixels = metrics.widthPixels;
  5. int heightPixels = metrics.heightPixels;
  6. System.out.println("width = " + widthPixels + " , height = " + heightPixels);
  1. //方法三:
  2. WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
  3. Display display = wm.getDefaultDisplay();
  4. Point point = new Point();
  5. display.getSize(point);
  6. int width = point.x;
  7. int height = point.y;
  8. System.out.println("width = " + width + " , height = " + height);

来自为知笔记(Wiz)

时间: 2024-10-08 04:48:55

Android 获取屏幕宽高值的相关文章

android获取屏幕宽高工具类

import java.lang.reflect.Field; import android.app.Activity; import android.content.Context; import android.graphics.Point; import android.util.DisplayMetrics; import android.view.Display; import android.view.Window; import android.view.WindowManager

android获取屏幕宽高与获取控件宽高

获取屏幕宽高 // 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px) int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p) Log.e(TAG + " getDefaultDisplay", "screen

android 获取屏幕宽高 和 获取控件坐标

一.获取屏幕宽高: (1). WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); (2). WindowManager wm = this.getWindowManager(); int width = wm

Android 获取屏幕宽高

WindowManager m = getActivity().getWindowManager(); DisplayMetrics metric = new DisplayMetrics(); m.getDefaultDisplay().getMetrics(metric); int width = metric.widthPixels; // 屏幕宽度(像素) int height = metric.heightPixels; // 屏幕高度(像素) float density = metr

Android获取屏幕宽高的方法

1. 实现代码 private int mWidth; private int mHeight; @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public void getDisplayMetrics() { WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDis

android 获取屏幕宽高最新方法

DisplayMetrics dm;  dm = new DisplayMetrics();  WindowManager wm = getContext().getWindowManager();  wm.getDefaultDisplay().getMetrics(dm);  int w = dm.widthPixels;  int h = dm.heightPixels;

android获取view宽高的几种方法

在onCreate方法中我们通过mView.getWidth()和mView.getHeight()获取到的view的宽高都是0,那么下面几种方法就可以在onCreate方法中获取到view的宽高. 1. int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED

获取屏幕宽高和系统色系

ios7下获取的屏幕高度    [UIScreen mainScreen].bounds.size.height=1024    ios8下获取的屏幕高度    [UIScreen mainScreen].bounds.size.height=768 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {        height =[UIScreen mainScreen].bounds.size.height;

获取屏幕宽高width(),outerWidth,innerWidth,documentElement.clientWidth, body.clientWidth的区别

基本介绍 $(window).width()与$(window).height() $(window).width()与$(window).height():获得的是屏幕可视区域的宽高,不包括滚动条与工具条. $(window).width() = width + padding $(window).height() = height + padding document.documentElement.clientWidth与document.documentElement.clientHei