要使TableRow中的单元格具有相等的宽度,您需要为每个单元格添加一个layout_weight的属性。确保将它们设置为相同的值。以下是一个示例:
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView tv1 = new TextView(this);
tv1.setText("Column 1");
tv1.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.WRAP_CONTENT, 1f));
TextView tv2 = new TextView(this);
tv2.setText("Column 2");
tv2.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.WRAP_CONTENT, 1f));
tr.addView(tv1);
tr.addView(tv2);
在此示例中,layout_weight设置为1f,这意味着每个单元格将均分TableRow的宽度。
这样,您就可以在您的自定义TableRow中获得等宽的单元格。