如何更改网格视图项目的高度取决于视图的高度?

时间:2019-04-01 19:16:57

标签: android gridview

我想绘制8x9或3x2等动态小方块。

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

我该怎么做?有任何建议或示例代码吗?

1 个答案:

答案 0 :(得分:0)

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;

使用显示指标获取设备屏幕的尺寸,然后可以使用屏幕的宽度和高度来构建视图的宽度和高度

相关问题