我在我的应用程序中以编程方式添加了表格布局。第一列包含图像视图,第二列包含文本视图,如下面的代码所示
for (int i = 0; i <23; i++) {
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
row.setLayoutParams(lp);
tv = new TextView(this);
tv.setText(array[i]);
ImageView image65 = new ImageView(this);
image65.setBackgroundResource(R.drawable.ic_no);
row.addView(tv,1);
row.addView(image65,0);
ll.addView(row,i);
}
一切都很好,除了我想在sp中指定图像视图的宽度和长度。如何在行的布局 - 旁边的图像视图中添加layoutprams?我尝试了以下但是没有成功。
for (int i = 0; i <23; i++) {
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
row.setLayoutParams(lp);
TableRow.LayoutParams lp2 = new TableRow.LayoutParams(customwidth,customheight);
tv = new TextView(this);
tv.setText(array[i]);
ImageView image65 = new ImageView(this);
image65.setBackgroundResource(R.drawable.ic_no);
image65.setLayoutParams(lp2);
row.addView(tv,1);
row.addView(image65,0);
ll.addView(row,i);
}
我找到了解决方案
for (int i = 0; i <23; i++) {
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
row.setLayoutParams(lp);
tv = new TextView(this);
tv.setText(array[i]);
ImageView image65 = new ImageView(this);
Drawable d = getResources().getDrawable(R.drawable.ic_yes);
image65.setImageDrawable(d);
image65.setLayoutParams(new TableRow.LayoutParams(40, 40));
row.addView(tv,1);
row.addView(image65,0);
ll.addView(row,i);
}