开发者

How to set the max height of a Layout in android?

开发者 https://www.devze.com 2023-03-17 19:16 出处:网络
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 th

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 w开发者_C百科orkaround?


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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消