2012-03-01 33 views
10

Tôi đang cố gắng tìm hiểu cách tự động thêm cột và hàng vào tablelayout.Cách tự động tạo cột trong tablelayout?

Tôi có ví dụ đơn giản này. Tuy nhiên, nó chỉ hiển thị cột đầu tiên khi chạy.

Ai đó có thể cho tôi biết những gì còn thiếu để hiển thị bốn cột thay vì một cột?

package com.apollo.testtablelayout; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class TestTableLayoutActivity extends Activity { 
    /** Called when the activity is first created. */ 

    String col1; 
    String col2; 
    String col3; 
    String col4; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1); 

     for (int x = 1; x < 10; x++) { 

      col1 = "(" + x + ") Column 1"; 
      col2 = "(" + x + ") Column 2"; 
      col3 = "(" + x + ") Column 3"; 
      col4 = "(" + x + ") Column 4"; 

      TableRow newRow = new TableRow(this); 

      TextView column1 = new TextView(this); 
      TextView column2 = new TextView(this); 
      TextView column3 = new TextView(this); 
      TextView column4 = new TextView(this); 

      column1.setText(col1); 

      newRow.addView(column1); 
      newRow.addView(column2); 
      newRow.addView(column3); 
      newRow.addView(column4); 

      tl.addView(newRow, new TableLayout.LayoutParams()); 
     } 
    } 

} 

Trả lời

3

Bạn cần phải có setText cho mỗi văn bản cột

column1.setText(col1); 
column2.setText(col2); 
column3.setText(col3); 
column4.setText(col4); 
0

Đừng quên rằng mỗi cột cần TableLayout.LayoutParams mới() là tốt, đó là sai lầm tôi đã cố gắng để thêm chế độ xem phức tạp hơn vào bố cục bảng.

newRow.addView(view, new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 
          TableRow.LayoutParams.WRAP_CONTENT)); 
Các vấn đề liên quan