java - Trouble laying out ImageViews programmatically in RelativeLayout -
i'm trying add 5 or imageviews relativelayout, each new 1 being right of previous one. however, (apparently) of them stacked on each other, if align_parent_left. relevant code snippet:
imageview[] foo = new imageview[levels]; (int = 0; < levels; i++) { foo[i] = new imageview(context); layoutfoo.addview(foo[i]); foo[i].setid(10 + i); if (i == 0) { relativelayout.layoutparams fooparams = (relativelayout.layoutparams) foo[i].getlayoutparams(); fooparams.addrule(relativelayout.align_parent_left); foo[i].setlayoutparams(fooparams); } else { relativelayout.layoutparams fooparams = (relativelayout.layoutparams) foo[i].getlayoutparams(); fooparams.addrule(relativelayout.right_of, foo[i-1].getid()); foo[i].setlayoutparams(fooparams); } }
any hints i'm doing wrong? shouldn't matter imageviews don't have width (because haven't assigned bitmap them) yet, right?
edit: problem turned out not setting height , width imageview. solution set them either setlayoutparams() or constructor in justwork's example.
you must add imageviews after setting properties them.
move code end of loop:
layoutfoo.addview(foo[i]);
by way don't need imageview[]
. can accomplish 1 imageview.
update:
here code can same thing with:
imageview foo; relativelayout.layoutparams fooparams; (int = 0; < levels; i++) { foo = new imageview(context); foo.setid(10 + i); fooparams = (relativelayout.layoutparams) foo.getlayoutparams(); // don't understand why trying params of hasn't been specified before. // if you, like: fooparams = new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); if (i == 0) { fooparams.addrule(relativelayout.align_parent_left); } else { fooparams.addrule(relativelayout.right_of, foo.getid()-1); } foo.setimagebitmap(yourbitmap); // set bitmap. foo.setlayoutparams(fooparams); layoutfoo.addview(foo); }
Comments
Post a Comment