how to set image width height based on screen size in gridview

后端 未结 3 1868
旧时难觅i
旧时难觅i 2021-02-04 09:00

I want to show 3x3 sized gridview. I want to set the height and width based on device size. I am taking reference from this link.

MainActivity-

public cl         


        
3条回答
  •  醉话见心
    2021-02-04 09:26

    I use the following approach when I have to resize my Activitys, I assume the same is valid for your case:

    // I'm storing the size of the window in the display var, so I can then play around
    final Display display = getWindowManager().getDefaultDisplay();
    final Point size = new Point();
    display.getSize(size);
    
    // In your case, you'll probably need something like this:
    GridView gv = (GridView) findViewById(R.id.gridview);
    gv.setWidth((int) size.x * 0.75);    // Whis would resize the grid width to the 75%
    gv.setHeight((int) size.y * 0.5);    // Same with the height, but to the 50%
    

提交回复
热议问题