android - How to set `ImageView`'s height to match it's width while `layout_weight` is set? -


here deal: have gridview android:numcolumns="2". items in gridview imageviews android:layout_weight="1", half screen width. problem heightproperty, should equal width. tried play scaleype, far without success. how can set width of gridview elements fill half screen , set height equal width?

you have imageviews width programmatically , set height.

but if use img.getwidth(); eg. in oncreate(), return 0 because hasn't been drawn yet.

so have in oncreate():

imageview img = (imageview) findviewbyid(r.id.img);  viewtreeobserver vto = img.getviewtreeobserver();          vto.addonpredrawlistener(new viewtreeobserver.onpredrawlistener() {              public boolean onpredraw() {                  int x;                  img.getviewtreeobserver().removeonpredrawlistener(this);                  x = img.getmeasuredwidth();                  img.setlayoutparams(new linearlayout.layoutparams(x,x));                  return true;              }          }); 

this imageview's width after been drawn , set height equal it.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -