我需要实现这样的布局:如果它内部的内容很少,布局应该适应内容。如果内容超过某个大小,例如300dp,它应该设置为最大高度(300dp),并且可以滚动。是否有任何属性可以做到这一点?如果没有,是否有任何解决方案?
layout params
。例如,如果显示尺寸小于300dp
,则可以将height
设置为wrap content
。如果大于300dp
,则将height
设置为300dp
。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);
}
希望这篇文章能够帮到你。
maxHeight
。该解决方案也适用于任何类型的视图。请在 http://chintanrathod.com/maxheightscrollview-in-android-using-android-studio/ 阅读更多信息。 - Chintan Rathod