2012-03-01 15 views
10

मैं टेबललेआउट में कॉलम और पंक्तियों को गतिशील रूप से जोड़ने का तरीका समझने की कोशिश कर रहा हूं।किसी तालिका प्लेआउट में गतिशील रूप से कॉलम कैसे बनाएं?

मेरे पास यह सरल उदाहरण है। हालांकि, यह दौड़ने पर केवल पहला कॉलम दिखाता है।

क्या कोई मुझे बता सकता है कि एक के बजाय चार कॉलम प्रदर्शित करने के लिए क्या गुम है?

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()); 
     } 
    } 

} 

उत्तर

3

आप स्तंभ पाठ से प्रत्येक के लिए setText की आवश्यकता है

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

मत भूलना कि प्रत्येक स्तंभ नई TableLayout.LayoutParams) की जरूरत है (और साथ ही, उस गलती मैं कोशिश कर बनाया गया था एक टेबल लेआउट में एक और जटिल दृश्य जोड़ने के लिए।

newRow.addView(view, new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 
          TableRow.LayoutParams.WRAP_CONTENT)); 
संबंधित मुद्दे