Wednesday, 15 May 2013

android - Weight of programmatically added elements in TableRow doesn't work -



android - Weight of programmatically added elements in TableRow doesn't work -

so, have tablelayout 4 tablerow in 1 xml file. have xml file defining textview inserted in row (3 textview per row). i'd textview have same width , working when in same xml file (at begining textview "hard-coded") add together them programmatically, don't have same width.

here first xml file

<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/hometable" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/lmbg" android:clickable="true" android:padding="20dp" > <tablerow android:id="@+id/tablerow1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:minheight="200dp" > </tablerow> <tablerow android:id="@+id/tablerow2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:minheight="200dp" > </tablerow> <tablerow android:id="@+id/tablerow3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:minheight="200dp" > </tablerow> <tablerow android:id="@+id/tablerow4" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:minheight="200dp" > </tablerow> </tablelayout>

this textview item

<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="top" android:layout_weight="1" android:clickable="true" android:gravity="center" android:onclick="gotolist" android:textsize="14sp" />

and how add together them programmatically

for(int = 0; < mtopcategories.size(); i++){ category c = mtopcategories.get(i); tablerow row = ((tablerow)findviewbyid(rowid[ididx])) ; cat = (textview) getlayoutinflater().inflate(r.layout.cat_grid_item, null); int iconid = getresources().getidentifier(c.getslug().replace('-', '_'), "drawable", getpackagename()); cat.setcompounddrawableswithintrinsicbounds(0, iconid, 0, 0); int textid = getresources().getidentifier(c.getslug().replace('-', '_'), "string", getpackagename()); cat.settext(textid); cat.settag(c.getid().tostring()); row.addview(cat); }

try explicitly setting layout params on textviews this:

tablerow.layoutparams lparams = new tablerow.layoutparams(0, -2 /* wrap_content */, 0.33f /* 33% of width */); cat.setlayoutparams(lparams);

see http://developer.android.com/reference/android/widget/tablerow.layoutparams.html#tablerow.layoutparams%28int,%20int,%20float%29 more info.

android android-layout android-tablelayout

No comments:

Post a Comment