我在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);
问题是LinearLayout没有填充父对象(即TableRow)。附件中的图片说明了这个问题,如安卓的hirarchyViewer所示(绿色的矩形就是我的分数)。
谢谢。
发布于 2011-03-07 20:05:17
以编程方式创建布局时,需要为父容器提供子容器的布局说明。
// 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);
试一试:]
https://stackoverflow.com/questions/4602882
复制