Match_parent does not populate the parent!

I have a LinearLayout inside a TableRow. LinearLayout runs in 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 LinearLayout does not populate the parent element (which is a TableRow). Attached images illustrate the problem, as shown in Android hirarchyViewer (green rectangles are my labels).

LinearLayout image

Thanks.

+6
android layout
source share
1 answer

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

 // child element LinearLayout mainRowLayout = new LinearLayout(this); // parent element TableRow tr = new TableRow(this); // parent element instructions (layout params for main row) TableRow.LayoutParams lopForMainRow = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); tr.addView(mainRowLayout, lopForMainRow); 

Release it:]

+13
source share

All Articles