79

Android 将屏幕尺寸定义为 Normal Large XLarge 等。

它会自动在适当文件夹中的静态资源之间进行选择。我在我的 java 代码中需要有关当前设备的这些数据。DisplayMetrics 仅提供有关当前设备密度的信息。没有关于屏幕尺寸的信息。

我确实在这里的 grep 代码中找到了 ScreenSize 枚举 但是这对我来说似乎不适用于 4.0 SDK。有没有办法获取这些信息?

4

15 回答 15

123

将此代码复制并粘贴到您的代码Activity中,执行时将Toast显示设备的屏幕尺寸类别。

int screenSize = getResources().getConfiguration().screenLayout &
        Configuration.SCREENLAYOUT_SIZE_MASK;

String toastMsg;
switch(screenSize) {
    case Configuration.SCREENLAYOUT_SIZE_LARGE:
        toastMsg = "Large screen";
        break;
    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        toastMsg = "Normal screen";
        break;
    case Configuration.SCREENLAYOUT_SIZE_SMALL:
        toastMsg = "Small screen";
        break;
    default:
        toastMsg = "Screen size is neither large, normal or small";
}
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
于 2012-06-28T20:21:03.343 回答
64
private static String getScreenResolution(Context context)
{
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    return "{" + width + "," + height + "}";
}
于 2013-12-29T12:30:39.887 回答
15

确定屏幕尺寸:

int screenSize = getResources().getConfiguration().screenLayout &Configuration.SCREENLAYOUT_SIZE_MASK;
  switch(screenSize) {
      case Configuration.SCREENLAYOUT_SIZE_LARGE:
        Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
         break;
       case Configuration.SCREENLAYOUT_SIZE_NORMAL:
          Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
           break;
       case Configuration.SCREENLAYOUT_SIZE_SMALL:
           Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
           break;
       default:
           Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();

 }

确定密度:

int density= getResources().getDisplayMetrics().densityDpi;
   switch(density)
  {
  case DisplayMetrics.DENSITY_LOW:
     Toast.makeText(context, "LDPI", Toast.LENGTH_SHORT).show();
      break;
  case DisplayMetrics.DENSITY_MEDIUM:
       Toast.makeText(context, "MDPI", Toast.LENGTH_SHORT).show();
      break;
  case DisplayMetrics.DENSITY_HIGH:
      Toast.makeText(context, "HDPI", Toast.LENGTH_SHORT).show();
      break;
  case DisplayMetrics.DENSITY_XHIGH:
       Toast.makeText(context, "XHDPI", Toast.LENGTH_SHORT).show();
      break;
  }

参考: http ://devl-android.blogspot.in/2013/10/wifi-connectivity-and-hotspot-in-android.html

于 2013-10-24T05:18:49.343 回答
4

我认为这是一段非常简单的代码!

public Map<String, Integer> deriveMetrics(Activity activity) {
    try {
        DisplayMetrics metrics = new DisplayMetrics();

        if (activity != null) {
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        }

        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("screenWidth", Integer.valueOf(metrics.widthPixels));
        map.put("screenHeight", Integer.valueOf(metrics.heightPixels));
        map.put("screenDensity", Integer.valueOf(metrics.densityDpi));

        return map;
    } catch (Exception err) {
        ; // just use zero values
        return null;
    }
}

这种方法现在可以在任何地方独立使用。无论您想在哪里获取有关设备屏幕的信息,请执行以下操作:

        Map<String, Integer> map = deriveMetrics2(this);
        map.get("screenWidth");
        map.get("screenHeight");
        map.get("screenDensity");

希望这可能对那里的人有所帮助,并且可能会发现它更易于使用。如果我需要重新更正或改进,请不要犹豫,让我知道!:-)

干杯!!!

于 2016-04-25T05:13:06.917 回答
4
DisplayMetrics displayMetrics = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

int width = displayMetrics.widthPixels;

int height = displayMetrics.heightPixels;
于 2017-07-01T07:29:30.270 回答
4

获取 DisplayMetrics 的方法:

1.

val dm = activity.resources.displayMetrics
val dm = DisplayMetrics() 
activity.windowManager.defaultDisplay.getMetrics(dm)
  1. val dm = DisplayMetrics() val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager wm..defaultDisplay.getMetrics(dm)

然后得到

以每英寸点数表示的屏幕密度。可以是 DENSITY_LOW、DENSITY_MEDIUM 或 DENSITY_HIGH

dm.densityDpi

可用显示大小的绝对高度(以像素为单位)。

dm.heightPixels

可用显示大小的绝对宽度(以像素为单位)。

dm.widthPixels

X 维度上每英寸屏幕的精确物理像素。

dm.xdpi

Y 维度上每英寸屏幕的精确物理像素。

dm.ydpi

显示指标

于 2020-09-14T13:28:16.413 回答
3

您可以使用此代码获取以像素为单位的显示大小。

Display display = getWindowManager().getDefaultDisplay();
SizeUtils.SCREEN_WIDTH = display.getWidth();
SizeUtils.SCREEN_HEIGHT = display.getHeight();
于 2012-06-28T20:26:20.267 回答
2

你可以试试这个,它正在工作示例

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int ht = displaymetrics.heightPixels;
int wt = displaymetrics.widthPixels;

if ((getResources().getConfiguration().screenLayout &     Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();}

  else if ((getResources().getConfiguration().screenLayout &  Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
        .show();

  } else if ((getResources().getConfiguration().screenLayout &        Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
        .show();
 } else {
Toast.makeText(this,
        "Screen size is neither large, normal or small",
        Toast.LENGTH_LONG).show();
 }

 // Determine density
 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);
 int density = metrics.densityDpi;

 if (density == DisplayMetrics.DENSITY_HIGH) {
Toast.makeText(this,
        "DENSITY_HIGH... Density is " + String.valueOf(density),
        Toast.LENGTH_LONG).show();
 } else if (density == DisplayMetrics.DENSITY_MEDIUM) {
Toast.makeText(this,
        "DENSITY_MEDIUM... Density is " + String.valueOf(density),
        Toast.LENGTH_LONG).show();
 } else if (density == DisplayMetrics.DENSITY_LOW) {
Toast.makeText(this,
        "DENSITY_LOW... Density is " + String.valueOf(density),
        Toast.LENGTH_LONG).show();
 } else {
Toast.makeText(
        this,
        "Density is neither HIGH, MEDIUM OR LOW.  Density is "
                + String.valueOf(density), Toast.LENGTH_LONG)
        .show();
 }

 // These are deprecated
 Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
    .getDefaultDisplay();
 int  width = display.getWidth();
 int height = display.getHeight();
于 2013-09-13T13:44:34.103 回答
2

带装饰物(包括按钮栏):

private static String getScreenResolution(Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics(metrics);
    return "{" + metrics.widthPixels + "," + metrics.heightPixels + "}";
}

无装饰:

private static String getScreenResolution(Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
    return "{" + metrics.widthPixels + "," + metrics.heightPixels + "}";
}

区别在于Display 类的getMetrics()getRealMetrics()方法。

于 2020-09-14T12:49:23.517 回答
2

该代码将为您提供以下格式的结果:宽度 x 高度

String displayResolution = getResources().getDisplayMetrics().widthPixels + "x" + getResources().getDisplayMetrics().heightPixels;
于 2020-10-04T10:57:27.647 回答
1

西蒙-

不同的屏幕尺寸具有不同的像素密度。与 26 英寸电视相比,您手机上的 4 英寸显示屏可能有更多或更少的像素。如果我理解正确,他想检测当前屏幕是哪个尺寸组,小、正常、大和特大。我唯一能想到的就是检测像素密度并用它来确定屏幕的实际尺寸。

于 2012-06-28T20:30:00.643 回答
1

我的几个应用程序都需要这个,下面的代码是我解决这个问题的方法。仅显示 onCreate 中的代码。这是一个独立的应用程序,可以在任何设备上运行以返回屏幕信息。

setContentView(R.layout.activity_main);
    txSize = (TextView) findViewById(R.id.tvSize);

    density = (TextView) findViewById(R.id.density);
    densityDpi = (TextView) findViewById(R.id.densityDpi);
    widthPixels = (TextView) findViewById(R.id.widthPixels);
    xdpi = (TextView) findViewById(R.id.xdpi);
    ydpi = (TextView) findViewById(R.id.ydpi);

    Configuration config = getResources().getConfiguration();

    if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
        Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
        txSize.setText("Large screen");
    } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
        Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
                .show();
        txSize.setText("Normal sized screen");
    } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
        Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
                .show();
        txSize.setText("Small sized screen");
    } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
        Toast.makeText(this, "xLarge sized screen", Toast.LENGTH_LONG)
                .show();
        txSize.setText("Small sized screen");
    } else {
        Toast.makeText(this,
                "Screen size is neither large, normal or small",
                Toast.LENGTH_LONG).show();
        txSize.setText("Screen size is neither large, normal or small");
    }

    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);

    Log.i(TAG, "density :" + metrics.density);
    density.setText("density :" + metrics.density);

    Log.i(TAG, "D density :" + metrics.densityDpi);
    densityDpi.setText("densityDpi :" + metrics.densityDpi);

    Log.i(TAG, "width pix :" + metrics.widthPixels);
    widthPixels.setText("widthPixels :" + metrics.widthPixels);

    Log.i(TAG, "xdpi :" + metrics.xdpi);
    xdpi.setText("xdpi :" + metrics.xdpi);

    Log.i(TAG, "ydpi :" + metrics.ydpi);
    ydpi.setText("ydpi :" + metrics.ydpi);

和一个简单的 XML 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tvSize"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />
<TextView
    android:id="@+id/density"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />
<TextView
    android:id="@+id/densityDpi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />
<TextView
    android:id="@+id/widthPixels"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />
<TextView
    android:id="@+id/xdpi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />
<TextView
    android:id="@+id/ydpi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

于 2013-10-26T02:36:02.463 回答
0

如果您处于非活动状态,即 Fragment、Adapter、Model 类或任何其他不扩展的 java 类将Activity无法getResources()正常工作。您可以getActivity()在片段中使用或使用context您传递给相应类的内容。

mContext.getResources()

我建议让一个类说 Utils,它将具有用于常见工作的方法/方法。这样做的好处是,您可以在应用程序中调用此方法的任何位置使用单行代码获得所需的结果。

于 2017-11-13T14:57:42.553 回答
0

下面的算法可用于识别哪个类别将用于在静态资源之间进行挑选,也可以满足较新的 XX 和 XXX 高密度

    DisplayMetrics displayMetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    mDensityDpi = displayMetrics.densityDpi;
    mDensity = displayMetrics.density;
    mDisplayWidth = displayMetrics.widthPixels;
    mDisplayHeight = displayMetrics.heightPixels;

    String densityStr = "Unknown";
    int difference, leastDifference = 9999;

    difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_LOW);
    if (difference < leastDifference) {
        leastDifference = difference;
        densityStr = "LOW";
    }

    difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_MEDIUM);
    if (difference < leastDifference) {
        leastDifference = difference;
        densityStr = "MEDIUM";
    }

    difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_HIGH);
    if (difference < leastDifference) {
        leastDifference = difference;
        densityStr = "HIGH";
    }

    difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_XHIGH);
    if (difference < leastDifference) {
        leastDifference = difference;
        densityStr = "XHIGH";
    }

    difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_XXHIGH);
    if (difference < leastDifference) {
        leastDifference = difference;
        densityStr = "XXHIGH";
    }

    difference = Math.abs(mDensityDpi - DisplayMetrics.DENSITY_XXXHIGH);
    if (difference < leastDifference) {
        densityStr = "XXXHIGH";
    }

    Log.i(TAG, String.format("Display [h,w]: [%s,%s] Density: %s Density DPI: %s [%s]", mDisplayHeight, mDisplayWidth, mDensity, mDensityDpi, densityStr));
于 2019-04-05T07:22:02.813 回答
0

在 Kotlin 中获取屏幕分辨率或屏幕尺寸

fun getScreenResolution(context: Context): Pair<Int, Int> {
    try {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = wm.defaultDisplay
        val metrics = DisplayMetrics()
        display.getMetrics(metrics)
        val width = metrics.widthPixels
        val height = metrics.heightPixels
        //Log.d(AppData.TAG, "screenSize: $width, $height")
        return Pair(width, height)

    } catch (error: Exception) {
        Log.d(AppData.TAG, "Error : autoCreateTable()", error)
    }

    return Pair(0, 0)
}
于 2020-09-29T04:53:27.557 回答