Đây là một vấn đề phức tạp, tôi đặt lề thành textview trong một hàng của bố cục bảng. xem bên dưới:
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableRow tr = new TableRow(this);
tr.setBackgroundResource(R.color.rowColor);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(4, 4, 4, 4);
TextView tv = new TextView(this);
tv.setBackgroundResource(R.color.textviewColor);
tv.setText("hello");
tr.addView(tv, params);
TextView tv2 = new TextView(this);
tv2.setBackgroundResource(R.color.textviewColor);
tv2.setText("hi");
tr.addView(tv2, params);
tl.addView(tr);
setContentView(tl);
lớp cần nhập cho LayoutParams để sử dụng trong một hàng bảng là:
import android.widget.**TableRow**.LayoutParams;
điều quan trọng cần lưu ý là tôi đã thêm lớp cho hàng bảng. tương tự, nhiều lớp khác có sẵn để sử dụng LayoutParams như:
import android.widget.**RelativeLayout**.LayoutParams;
nhập android.widget. LinearLayout .LayoutParams;
vì vậy sử dụng cho phù hợp.