match_parent不填充父级!

ofi*_*rbt 7 android-layout

我在TableRow中有LinearLayout.LinearLayout在代码中启动,这样:

LinearLayout  mainRowLayout = new LinearLayout(this);
mainRowLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TableRow tr = new TableRow(this);
tr.addView(mainRowLayout);
Run Code Online (Sandbox Code Playgroud)

问题是LinearLayout没有填充父级(这是TableRow).附图说明了问题,如Android的hirarchyViewer(绿色矩形是我的标记)所示.

"LinearLayout图片"

谢谢.

ata*_*ulm 13

以编程方式创建布局时,您需要为父容器提供子项的布局说明.

// 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);
Run Code Online (Sandbox Code Playgroud)

搏一搏 :]