Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match_parent doesn't fill parent!

I have LinearLayout inside TableRow. The LinearLayout get initiated in the code, this way:

LinearLayout  mainRowLayout = new LinearLayout(this);
mainRowLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TableRow tr = new TableRow(this);
tr.addView(mainRowLayout);

The problem is that the LinearLayout doesn't fill the parent (which is the TableRow). The attached images illustrates the problem, as shown in the Android's hirarchyViewer (The green rectangles are my marks).

"LinearLayout image"

Thanks.

like image 209
ofirbt Avatar asked Jan 05 '11 10:01

ofirbt


1 Answers

When programmatically creating layouts, you need to give the parent container the layout instructions for the child.

// child element
LinearLayout mainRowLayout = new LinearLayout(this);

// parent element
TableRow tr = new TableRow(this);

// parent element's instructions (layout params for main row)
TableRow.LayoutParams lopForMainRow = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

tr.addView(mainRowLayout, lopForMainRow);

Give it a go :]

like image 80
ataulm Avatar answered Sep 22 '22 09:09

ataulm