Android layout 匹配父项不匹配';不要填家长!

Android layout 匹配父项不匹配';不要填家长!,android-layout,Android Layout,我在桌子里有一条直线。 在代码中启动LinearLayout,方法如下: LinearLayout mainRowLayout = new LinearLayout(this); mainRowLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); TableRow tr = new TableRow(this); tr.addView(mainRowLayo

我在桌子里有一条直线。 在代码中启动LinearLayout,方法如下:

LinearLayout  mainRowLayout = new LinearLayout(this);
mainRowLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TableRow tr = new TableRow(this);
tr.addView(mainRowLayout);
问题是LinearLayout没有填充父级(即TableRow)。 附加的图片说明了这个问题,如Android的hirarchyViewer所示(绿色矩形是我的标记)


谢谢。

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

// 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);
试一试:]