tags:

views:

83

answers:

2

Hi, I have the following xml object representing a ImageButton

<?xml version="1.0" encoding="utf-8"?>
<ImageButton
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/genreCellItemId" android:layout_weight="1"
  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"
  android:paddingLeft="5.0dip" android:paddingRight="5.0dip">
</ImageButton>

I try to inflate it and add it to a TableRow with the following code :

LayoutInflater inflater= this.getLayoutInflater();
TableLayout grid = (TableLayout)findViewById(R.id.bodyTableLayout);
TableRow row = (TableRow)inflater.inflate(R.layout.genre_row, null);
View myButton = (View)inflater.inflate(R.layout.genre_cell, null);
row.addView(tempButton, new TableRow.LayoutParams());
grid.addView(row, new TableLayout.LayoutParams());

Ok so I noticed that it didn't look as expected, so I fire up the Hierarchy Viewere and noticed that the ImageButton's actually have an layout_width = FILL_PARENT instead of WRAP_CONTENT, also the layout_gravity is NONE and there is no padding to be seen...

Do, am I inflating it wrongly ? Or is maybe the new TableRow.LayoutParams() part doins omething wrong ?

+2  A: 

Try using inflate(R.layout.genre_cell, row, false) instead of inflate(R.layout.genre_cell, null) and see if that helps. Sometimes, the inflation works better when it knows what sort of parent the inflated view will go into.

CommonsWare
unfortunately that didn't work.still no padding to be seen and the buttons are still "glued" one against the other.when using Hierarchy Viewer the layout_widt is still fill_parent and gravity is none. I do set a background after I inflate the ImageButton could that be a problem ? Also is the new LayoutParams() a problem ?
TiGer
I noticed that also my Row doesn't get inflated well :<TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/genreRowItemId" android:layout_height="wrap_content" android:layout_width="fill_parent" android:paddingBottom="5dp" android:paddingTop="5dp" android:gravity="center"></TableRow>This one also doens't get it's layout_gravity set to Center but stays on NONE :(
TiGer
A: 

Well after nearly two days I have found the culprit :

As I actually suspected the new LayoutParams() is a problem if you already have "formatted" your View in the XML declaration... So after leaving the new LayoutParams() out of the equation my Table would be shown just fine...

So just use :

grid.addView(row);
TiGer