在动态创建的表格行中对齐视图,可以使用布局参数来设置视图的对齐方式。下面是一个示例的解决方法,包含代码示例:
TableLayout tableLayout = new TableLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tableLayout.setLayoutParams(layoutParams);
layout.addView(tableLayout);
// 创建表格行
TableRow row = new TableRow(context);
// 创建视图
TextView textView = new TextView(context);
textView.setText("Hello");
Button button = new Button(context);
button.setText("Click me");
// 设置视图的对齐方式
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER; // 设置视图在表格行中居中对齐
// 将视图添加到表格行中
row.addView(textView, params);
row.addView(button, params);
// 将表格行添加到表格布局中
tableLayout.addView(row);
在上面的示例中,我们创建了一个TableLayout和一个TableRow,并将它们添加到布局中。然后,我们创建了一个TextView和一个Button,并设置它们的对齐方式为居中对齐。最后,我们将TextView和Button添加到TableRow中,并将TableRow添加到TableLayout中。
通过这种方式,我们可以在动态创建的表格行中对齐视图。