2013-11-27 55 views

回答

1

試試這個..

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
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)); 
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)); 
} 
相關問題