Thursday, 15 August 2013

java - Setting margin AND background color in a TableRow -



java - Setting margin AND background color in a TableRow -

i have unusual issue when setting programmatically both margin and background color on tablerow. read this thread margin, , this one background color, apparently, things correctly. problem seems somewhere else.

i create dynamically tablerows, odds must specific color (to improve readability). wrote :

if(mycursor != null && mycursor.movetofirst()){ do{ tablerow tablerow = new tablerow(getactivity()); tablelayout.layoutparams tlp = new tablelayout.layoutparams(tablelayout.layoutparams.match_parent, tablelayout.layoutparams.match_parent); tlp.setmargins(0, 10, 0, 10); tablerow.setlayoutparams(tlp); for(int j=0 ; j < toptagsdetailscursor.getcolumncount();j++){ textview tv = new textview(getactivity()); tv.settext(toptagsdetailscursor.getstring(j)); if (j>0) tv.setgravity(gravity.center); tablerow.addview(tv); } if ((i % 2) != 0){ tablerow.setbackgroundcolor(getresources().getcolor(r.color.table_row)); } datatable.addview(tablerow); i++; }); }while(mycursor.movetonext());

what don't understand, on each odd row, custom background color, custom margins (10 top , bottom) aren't "applied" row, on row. still stranger: if remove part of code custom background color, margins ok everywhere !!

so, why custom margins aren't ok when set custom background color ??

ps: tried move lines of code (especially addview(tablerow) , color), no effect.

thanks in advance ! :)

it doesn't explain why didn't work before, @ to the lowest degree found "fix". remove these lines :

tablelayout.layoutparams tlp = new tablelayout.layoutparams(tablelayout.layoutparams.match_parent, tablelayout.layoutparams.match_parent); tlp.setmargins(0, 10, 0, 10); tablerow.setlayoutparams(tlp);

and replaced :

tablerow.addview(tv);

by :

tablerow.addview(tv, new tablerow.layoutparams(tablerow.layoutparams.match_parent, 80)); //or other value

and works fine finally... if don't understand why first solution didn't work :) (feel free explain me why !)

java android

No comments:

Post a Comment