如何知道安卓设备的最小宽度(sw)?

30

我有4个不同的设备:

  1. Asus平板电脑,屏幕尺寸为7英寸
  2. Lenovo平板电脑,屏幕尺寸为7英寸
  3. HTC手机,屏幕尺寸为5英寸
  4. HTC手机,屏幕尺寸为4.7英寸

我想知道我设备的最小宽度(sw),以便为其创建支持布局。

我想创建一个名为“layout-sw600dp”的资源文件夹,但我不知道最小宽度(sw)的值。

我尝试使用以下代码打印sw:

DisplayMetrics metrics = getResources().getDisplayMetrics();
Log.i("Density", ""+metrics.densityDpi);

但我不知道这是否是正确的值。

我该如何找到最小宽度(sw)?


通常只要提供正确的资源文件,您就不需要知道太多。请参见此答案 - Suragch
5个回答

63

22

这里有另一种简单的方法。

您可以从开发人员选项中检查设备的最小宽度(在Android Studio模拟器中,但在我的真实设备上找不到)。

输入图片描述


13

以上答案有误,正确的代码以查找正确的软件,请尝试以下代码:

DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
float screenWidth = dm.widthPixels / dm.density;
float screenHeight = dm.heightPixels / dm.density;

屏幕宽度和屏幕高度中较小的那个是你的sw。


看起来这是正确的答案。接受的答案在Note 5上给了我2.79。Note 5正在从我的sw360dp文件夹中获取值,所以那肯定不是确定最小宽度的准确方法。希望Google能够在某个官方文档中提供公式,以便让我放心,而不是让我在StackOverflow上搜索... - welshk91
我们不需要像(px * 160)/dpi那样乘以160吗? - H Raval

10

最低支持API 13及以上,采用@Tobliug的回答是最佳解决方案。

 Configuration config = getResources().getConfiguration();
 config.smallestScreenWidthDp;// But it requires API level 13

如果API级别低于13,请尝试以下答案

创建 SmallWidthCalculator.java 类并将以下代码复制粘贴

import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;

 public class SmallWidthCalculator {
private static SmallWidthCalculator ourInstance = new SmallWidthCalculator();

public static SmallWidthCalculator getInstance() {
    return ourInstance;
}

private Context mContext;

private SmallWidthCalculator() {
}

public double getSmallWidth(Context context) {
    mContext = context;
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    int width = dm.widthPixels;
    int height = dm.heightPixels;


    double dpi = getDPI(width, height);


    double smallWidthDPI = 0;
    int smallWidth = 0;

    if (width < height)
        smallWidth = width;
    else
        smallWidth = height;

    smallWidthDPI = smallWidth / (dpi / 160);

    return smallWidthDPI;
}

private double getDPI(int width,
                      int height) {
    double dpi = 0f;
    double inches = getScreenSizeInInches(width, height);
    dpi = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / inches;
    return dpi;
}

private double getScreenSizeInInches(int width, int height) {
    if (mContext != null) {
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        double wi = (double) width / (double) dm.xdpi;
        double hi = (double) height / (double) dm.ydpi;
        double x = Math.pow(wi, 2);
        double y = Math.pow(hi, 2);
        return Math.sqrt(x + y);
    }
    return 0;
}

}

从你的Activity或Fragment中传递上下文,获取你的小宽度。

double smallWidthDp=SmallWidthCalculator.getInstance().getSmallWidth(this);

6
你可以尝试这个方法:
DisplayMetrics dm = mActivity.getApplicationContext()
                    .getResources().getDisplayMetrics();
            float screenWidth = dm.widthPixels / dm.xdpi;
            float screenHeight = dm.heightPixels / dm.ydpi;

详细信息请查看:这里


是的,您可以在运行时找到,但必须在编译之前创建您的文件夹。那么当您想要使用sw而不是mdpi、hdpi等时,sw是什么桶? - Mahdi
7
这不会返回SW。应该是screenWidth = dm.widthPixels / dm.density; - VSB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接