我正在使用此代码在相对布局中动态显示表。
我可以在调试模式下看到相对布局和表格布局的子项,但它在屏幕上不可见。
我做错了什么?
代码是:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.my_relative_layout);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
tableLayout = new TableLayout(MyActivity.this);
tableLayout.setBackgroundColor(Color.BLACK);
TableRow.LayoutParams tableHeaderRowParams = new TableRow.LayoutParams();
tableHeaderRowParams.setMargins(5, 5, 5, 5);
tableHeaderRowParams.weight = 0;
tableHeaderRowParams.span = 2;
TableRow tableHeaderRow = new TableRow(MyActivity.this);
tableHeaderRow.setBackgroundColor(Color.WHITE);
TextView txvHeader = new TextView(MyActivity.this);
txvHeader.setLayoutParams(tableHeaderRowParams);
txvHeader.setBackgroundColor(Color.WHITE);
txvHeader.setGravity(Gravity.CENTER);
txvHeader.setTag(10);
txvHeader.setId(10);
txvHeader.setTextColor(Color.BLUE);
txvHeader.setTextSize(TypedValue.COMPLEX_UNIT_SP, rowTextSize);
txvHeader.setTypeface(null, Typeface.BOLD);
txvHeader.setText(“Header”);
tableHeaderRow.addView(txvHeader, tableHeaderRowParams);
tableLayout.addView(tableHeaderRow, tableLayoutParams);
RelativeLayout.LayoutParams lpTable = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTable.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
lpTable.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
lpTable.topMargin = 9 * space + 6 * rowHeight;
relative.addView(tableLayout, lpTable);
谢谢!