LayoutInflater ignoring parameters?
I have the following xml object representing 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 am trying to inflate it and add it to 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 it doesn't look as expected, so I run the Hierarchy Viewere and noticed that the ImageButton actually has layout_width = FILL_PARENT instead of WRAP_CONTENT, also layout_gravity is NONE, and there is no padding seen ...
Am I inflating it wrong? Or maybe the new TableRow.LayoutParams () part is doing something wrong?
a source to share
Two days later, I found the culprit:
As I did suspect that the new LayoutParams () is a problem if you have already "formatted" your view in an XML declaration ... So leaving the new LayoutParams () out of the equation will make my table show just fine ...
So just use:
grid.addView(row);
a source to share
For anyone using Data Binding x Kotlin and facing this problem:
instead:
val binding = YourBindingClass.inflate(LayoutInflater.from(parent.context))
return ViewHolder(binding)
use this one:
val binding = YourBindingClass.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
a source to share