我想动态地向表布局中添加行。这是我的代码。
TableRow tableRow = null;
TextView textView = null;
ImageView imageView = null;
TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout);
RelativeLayout relativeLayout = null;
for (String string: listOfStrings) {
tableRow = new TableRow(this);
relativeLayout = (RelativeLayout) View.inflate(this,
R.layout.row, null);
textView = (TextView) relativeLayout.getChildAt(0);
textView.setText(string);
imageView= (ImageView) relativeLayout.getChildAt(1);
imageView.setBackgroundColor(R.color.blue);
tableRow.addView(relativeLayout);
tableLayout.addView(tableRow);
}我已经创建了一个宽度为fill_parent的行布局,textView在最左侧,imageView在最右侧。但是,当我运行这个程序时,行宽显示为wrap_content,而不是带有图像重叠文本的fill_parent。请帮帮忙。谢谢
发布于 2011-08-03 02:16:53
我注意到的第一件事是在添加视图时没有设置任何LayoutParams。
TableRow tableRow = null;
TextView textView = null;
ImageView imageView = null;
TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout);
RelativeLayout relativeLayout = null;
for (String string: listOfStrings)
{
tableRow = new TableRow(this);
relativeLayout = (RelativeLayout) View.inflate(this, R.layout.row, null);
textView = (TextView) relativeLayout.getChildAt(0);
textView.setText(string);
imageView= (ImageView) relativeLayout.getChildAt(1);
imageView.setBackgroundColor(R.color.blue);
//Here's where I would add the parameters...
TableLayout.LayoutParams rlParams = new TableLayout.LayoutParams(FILL_PARENT, WRAP_CONTENT);
tableRow.addView(relativeLayout, rlParams);
tableLayout.addView(tableRow);
}在实践中,我还会将行的创建抽象为它们自己的方法。以简化使用/重用。
https://stackoverflow.com/questions/3830791
复制相似问题