检查 screenSize 不起作用

Checking screenSize is not working

这是我用于检查屏幕尺寸和更改 FrameLayout 尺寸的代码。

    FrameLayout topLayout = (FrameLayout) findViewById(R.id.topLayout);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(dm.widthPixels/dm.xdpi,2);
    double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
    double screenInches = Math.sqrt(x + y);

    if (screenInches < 5) topLayout.setLayoutParams(new LinearLayout
    .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,170));
    if (screenInches >= 5 && screenInches < 7) topLayout
    .setLayoutParams(new LinearLayout 
    .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));
    else topLayout.setLayoutParams(new LinearLayout
    .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 230));

我不知道为什么它不起作用。

DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity)mContext)getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

要更改布局参数,您必须这样做:

topLayout.getLayoutParams().width = 100;
topLayout.getLayoutParams().height = 100;

您可以使用 DisplayMetrics .

A structure describing general information about a display, such as its size, density, and font scaling.

   DisplayMetrics metrics = getResources().getDisplayMetrics();

   int DeviceTotalWidth = metrics.widthPixels;
   int DeviceTotalHeight = metrics.heightPixels;
   FrameLayout topLayout = (FrameLayout) findViewById(R.id.topLayout);

   topLayout.getLayoutParams().height= (DeviceTotalHeight/4); // Use Your Logic

现在您可以简单地使用您的 screenInches 逻辑。