如何根据视图的高度更改网格视图项目的高度?



我想画动态的小方块,如 8x9 或 3x2 等。

此网格项的高度和宽度必须相等,并且应根据视图的大小进行更改。

我该怎么做?请问任何建议或示例代码?

DisplayMetrics displaymetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    //if you need three fix imageview in width
    int devicewidth = displaymetrics.widthPixels / 3;
    //if you need 4-5-6 anything fix imageview in height
    int deviceheight = displaymetrics.heightPixels / 4;
    holder.image_view.getLayoutParams().width = devicewidth;
    //if you need same height as width you can set devicewidth in holder.image_view.getLayoutParams().height
    holder.image_view.getLayoutParams().height = deviceheight;
holder.image_view.getLayoutParams().height = devicewidth;

使用显示指标获取设备屏幕的大小,您可以使用屏幕的宽度和高度来构建视图的宽度和高度

最新更新