如何以编程方式在Android设备上获取总屏幕大小

时间:2013-11-28 10:02:09

标签: android user-interface screen

如果我使用此处显示的代码来获得总屏幕尺寸,则总是将高度缩短到总屏幕尺寸,如设备的文档规格所示。例如,我试图通过使用代码来获得屏幕大小,以获得列为1280 X 800的平板电脑的大小,代码的结果是:1216 X 800.那么缺少64个像素的位置在哪里?

我可以猜到它可能是屏幕底部的条形,它保持背部和主页按钮。但这听起来不对,因为内容视图应该是所有视图的根。这是怎么回事?

唯一可能的解释可能是UI的这一部分

is this the exception to total screen size?

用于获取屏幕尺寸的代码

  // gets the content view windows width and height size
   DisplayMetrics displaymetrics = new DisplayMetrics();
   getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
   int widthContentView = displaymetrics.widthPixels;
   int heightContentView = displaymetrics.heightPixels;
   Toast.makeText(MainActivity.this, "width of content view: " + widthContentView  Toast.LENGTH_SHORT).show();
   Toast.makeText(MainActivity.this, "height of content view: " + heightContentView, Toast.LENGTH_SHORT).show();

8 个答案:

答案 0 :(得分:7)

如果您在Activity之外调用它,则需要传递上下文(或通过其他调用获取)。然后使用它来获取显示指标:

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

答案 1 :(得分:2)

我不确定你使用的是什么api但是如果你从17开始使用api就可以使用它:

display.getRealSize();

答案 2 :(得分:2)

try this code...

Display display = context.getWindowManager()
            .getDefaultDisplay();
        int width = display.getWidth();
    int height=display.getHeight();


it will give screen's width and height,And according to width and height write if-    conditions for each device's screen resolution. 

答案 3 :(得分:1)

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();
}

答案 4 :(得分:1)

你是对的,它向你展示你的应用程序屏幕的分辨率,因此你得到了差异。而不是getMetrics(),使用getRealMetrics()。 您可以使用以下代码获取设备的实际屏幕尺寸。

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    DisplayMetrics metrics = new DisplayMetrics();
    display.getRealMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    return width + "*" + height;

答案 5 :(得分:0)

int density;
    private DisplayMetrics metrics;
    private int widthPixels;
    private float scaleFactor;
    private float widthDp;
metrics = new DisplayMetrics();
                getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

                density= getResources().getDisplayMetrics().densityDpi;

                widthPixels = metrics.widthPixels;

                scaleFactor = metrics.density;

                widthDp = widthPixels / scaleFactor;
System.out.println("widthDp== "+widthDp );
if(density==DisplayMetrics.DENSITY_HIGH)
                        System.out.println("Density is high");

                    if(density==DisplayMetrics.DENSITY_XXHIGH)
                        System.out.println("Density is xxhigh");

                    if(density==DisplayMetrics.DENSITY_XXXHIGH)
                        System.out.println("Density is xxxhigh");

                    if(density==DisplayMetrics.DENSITY_TV)
                        System.out.println("Density is Tv");

答案 6 :(得分:0)

    int Measuredwidth = 0;
    int Measuredheight = 0;
    Point size = new Point();
    WindowManager w = getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        w.getDefaultDisplay().getSize(size);
        Measuredwidth = size.x;
        Measuredheight = size.y;
    } else {
        Display d = w.getDefaultDisplay();
        Measuredwidth = d.getWidth();
        Measuredheight   = d.getHeight();;
    }

答案 7 :(得分:-1)

根据需要为所有显示设置您的宽度和高度。

    Display display;
    display=getWindowManager().getDefaultDisplay();
    text1.setWidth(display.getWidth()-380);
    text1.setHeight(display.getHeight()-720);