android - Programmatically adding ABOVE to LayoutParams causes view height to be 0 -
i have custom view
. in constructor view, create , add 2 subviews. however, using layoutparams.addrule()
causing problems. rules such center_horizontal
work, when try use above
, subview ends height of 0.
here code in constructor:
setlayoutparams(new layoutparams(layoutparams.match_parent, layoutparams.match_parent)); mlayoutparams = new layoutparams(width, height); mlayoutparams.leftmargin = left; mlayoutparams.topmargin = top; mimage = new imageview(getcontext()); mimage.setimagedrawable(getresources().getdrawable(r.drawable.my_image_drawable)); mimage.setscaletype(scaletype.fit_xy); mimage.setid(r.id.my_image_id); addview(mimage, mlayoutparams); mtext = new textview(getcontext()); mtext.settext(r.string.my_text); mtext.setid(r.id.my_text_id); layoutparams textparams = new layoutparams(200, 40); // textparams.addrule(center_horizontal, true); //works textparams.addrule(above, mimage.getid()); //doesn't work addview(mtext, textparams);
if inspect view on emulator device monitor, can see layout_height
of text 40, top, bottom, , height parameters 0. measuredheight
appears same screen width.
- am doing wrong adding rules?
- is there better place add rules? need happen first?
- any other alternatives? making
textview
have height of fill_parent, gravity bottom, , setting bottom margin height of image works, need able add view above text, , won't work that.
where in code checking height? if it's somewhere within oncreate or oncreateview, won't info. layout two-pass process , in 2 methods 2 passes not complete.
you may need height in methods using ".post" method this:
myview.post( new runnable() { @override public void run() { int height = myview.getheight(); } });
the runnable makes run on ui thread, it's invoked after both passes, measure pass , layout pass, have completed. trying set rule based on center horizontal without first knowing measurements going fail.
Comments
Post a Comment