将行和单元格添加到TableLayout中

时间:2013-11-27 15:54:17

标签: android android-tablelayout

如何执行以下操作:

  1. 以编程方式在TableLayout中创建一行
  2. 在行中创建三个单元格
  3. 在每个单元格中添加任意文字
  4. 感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

试试这个..

TableLayout tableLayout = new TableLayout(this);
          tableLayout.setStretchAllColumns(true);
          tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
          tableLayout.setWeightSum(3);

          for (int i = 0; i < 3; i++) {
              TableRow tableRow = new TableRow(this);
              tableRow.setGravity(Gravity.CENTER);
              tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));

              for (int j = 0; j < 3; j++) {
                  TextView button = new TextView(this);
                  final int buttonNumber = (j + i * 4);
                  button.setText("" + buttonNumber);
                  button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));

                  tableRow.addView(button);
              }
              tableLayout.addView(tableRow);
          }

          main_lay.addView(tableLayout);

答案 1 :(得分:1)

TableLayout table = (TableLayout) findViewById(R.id.TableName);

TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

TextView cell1= new TextView(getApplicationContext());
                cell1.setText("one");
                cell1.setTextColor(Color.BLACK);
                cell1.setTextSize(16f);
                cell1.setTypeface(tf);

TextView cell2= new TextView(getApplicationContext());
                cell2.setText("two");
                cell2.setTextColor(Color.BLACK);
                cell2.setTextSize(16f);
                cell2.setTypeface(tf);

TextView cell3= new TextView(getApplicationContext());
                cell3.setText("three");
                cell3.setTextColor(Color.BLACK);
                cell3.setTextSize(16f);
                cell3.setTypeface(tf);

 tableRow.addView(cell1);
 tableRow.addView(cell2);
 tableRow.addView(cell3);

 talbe.addView(tableRow , new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

答案 2 :(得分:0)

如果你想要,你可以使用这样的函数来更新你的tableLayout,它会添加一个带有一些文本的新行。

 private void addRowToTableLayout(TableLayout tableLayout, String text){

 TableRow tr = new TableRow(MyActivity.this);
 //if you want you can customize the table row
 tr.setBackgroundColor(getResources().getColor(R.color.backgroundColor));
 tr.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

 TextView textView = new TextView(MyActivity.this);
 //if you want you can customize the textview also
 textView.setText(text);
 tr.addView(textView);
 tableLayout.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                                    LayoutParams.WRAP_CONTENT));
}