【发布时间】:2014-02-28 13:44:40
【问题描述】:
我正在尝试动态生成我的活动布局,但由于某种原因,我无法让我的 TableLayout 或我的 TableRow 与父宽度匹配。我使用的是相对布局,然后我创建了我的 TableLayout 并在将我的 TableLayout 添加到我的 RelativeLayout 之前用行填充它。
这是我的 onCreate:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = this;
RelativeLayout mainLayout = new RelativeLayout(context);
RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
mainLayout.setLayoutParams(relativeLayout);
//Create the signature views
//createPage(mainLayout);
setupPage(mainLayout);
setContentView(mainLayout);
}
这是我的setupPage() 代码:
private void setupPage(RelativeLayout mainLayout)
{
TableLayout mainTable = new TableLayout(context);
RelativeLayout.LayoutParams tableLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
mainLayout.setLayoutParams(tableLayoutParams);
mainTable.setBackgroundResource(R.drawable.rectangle);
//JOB DESCRIPTION title
mainTable.addView(getRowTitle("JOB DESCRIPTION"));
//---- END ----
mainLayout.addView(mainTable);
}
private TableRow getRowTitle(String text)
{
TableRow row = new TableRow(context);
TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT
);
row.setLayoutParams(rowLayout);
row.setBackgroundResource(R.drawable.rectangle);//so i can see the outline of the row
/*TextView title = new TextView(context);
TableRow.LayoutParams editLayout = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT
);
editLayout.setMargins(15,0,0,0);
title.setLayoutParams(editLayout);
title.setText(text);
row.addView(title);*/
return row;
}
【问题讨论】:
-
您将
tableLayoutParams(类型为TableLayout.LayoutParams)的父RelativeLayout设置为LayoutParams是否有原因? -
我认为孩子必须拥有父母的布局参数?我试过使用 TableLayout.LayoutParams 但这并没有改变任何东西
-
上面评论中的一个小错误,而不是在
mainLayout上设置LayoutParams(setupPage()方法中的mainLayout.setLayoutParams(tableLayoutParams);)设置在实际的TableLayout上:@987654333 @ -
啊哈。这就是问题所在。我不小心得到了
mainLayout.setLayoutParams而不是mainTable.setLayoutParams.........你能把它作为答案发布以便我接受吗?谢谢你发现我的愚蠢哈哈
标签: android android-relativelayout tablerow android-tablelayout