如何在android中设置布局的最大高度?

时间:2022-08-24 07:35:11

I have to implement layout like this; The layout should wrap the content if the content within it is less. If the content goes beyond some size, like 300dp, it should be set to max heght(300dp) and that be able to scroll. Is there any properties to do like this, if not any workaround?

我必须像这样实现布局;如果内容中的内容较少,则布局应包装内容。如果内容超出某个大小,如300dp,则应将其设置为max heght(300dp)并且能够滚动。是否有任何属性可以这样做,如果没有任何解决方法?

1 个解决方案

#1


7  

You can find the display size of the device. according to the display size you can set layout params. for example, if the display size is less than 300dp , you can set height as wrap content. if more than 300dp means set height as 300dp.

您可以找到设备的显示尺寸。根据显示尺寸,您可以设置布局参数。例如,如果显示大小小于300dp,则可以将高度设置为换行内容。如果超过300dp则意味着设置高度为300dp。

here is the example code:

这是示例代码:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);


    DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);


    //here you can get height of the device.
   Log.d("check", metrics.heightPixels+"");
    if(metrics.heightPixels< 300)
    {
         this.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    }
    else {
         this.getWindow().setLayout(LayoutParams.WRAP_CONTENT,300);

    }

hope this one can help you.

希望这个可以帮助你。

#1


7  

You can find the display size of the device. according to the display size you can set layout params. for example, if the display size is less than 300dp , you can set height as wrap content. if more than 300dp means set height as 300dp.

您可以找到设备的显示尺寸。根据显示尺寸,您可以设置布局参数。例如,如果显示大小小于300dp,则可以将高度设置为换行内容。如果超过300dp则意味着设置高度为300dp。

here is the example code:

这是示例代码:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);


    DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);


    //here you can get height of the device.
   Log.d("check", metrics.heightPixels+"");
    if(metrics.heightPixels< 300)
    {
         this.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    }
    else {
         this.getWindow().setLayout(LayoutParams.WRAP_CONTENT,300);

    }

hope this one can help you.

希望这个可以帮助你。