When doing a project, an app often needs a ' live ' display instead of a fixed width, such as when using the GridView to display pictures:
Of course, the number of columns is fixed, but what we need is that the aspect ratio of the picture will not become, the size of the picture is still to be done. So there is the following configuration
Assume that the aspect ratio of the picture is 16:9:
1Item_relatlayout =(relativelayout) Itemview.findviewbyid (r.id.item_relatlayout);2ImageView =(ImageView) Itemview.findviewbyid (R.id.imageview);3PLAYBTN =(TextView) Itemview.findviewbyid (r.id.play_tub);4Del_box =(CheckBox) Itemview.findviewbyid (r.id.del_ckbox);5 intPxwidth =Util.getscreanwith (context);6 intPxmargin = util.dp2px (context,0.1f);7Gridlayoutmanager.layoutparams Layoutparams =(Gridlayoutmanager.layoutparams) item_relatlayout.getlayoutparams ();8 Layoutparams.setmargins (pxmargin,pxmargin,pxmargin,pxmargin);9 Item_relatlayout.setlayoutparams (layoutparams);Ten //total width minus values for all gaps One intt = pxwidth-pxmargin*6; A //re-calculate the width of the picture -Relativelayout.layoutparams params =(Relativelayout.layoutparams) imageview.getlayoutparams (); -Params.width = T/3; theParams.height = (int) ((T/3) * (0.5) ;//Calculate the aspect ratio -Imageview.setlayoutparams (params);
Only in this way, the size of the image will be changed with different screens, but the size ratio is still unchanged ...
Custom screen-fit recipes