2011-07-12 101 views
2

我想在表格佈局中顯示數據。數據的數量是動態的,所以我需要在表中實現動態行。爲此,我想在單行中顯示4個TextView。在表格佈局中顯示動態行的問題:Android

問題

目前,我能夠證明表和四個TextView的數據,但我想所有四個的EditText之間的一些空間,但無法做到這一點。

這裏是我的代碼

for(ItemRow row : this.getRows()) { 

       if(row != null) { 
        // delcare a new row 
        newRow = new TableRow(this); 
        newRow.setLayoutParams(new LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.WRAP_CONTENT)); 
        newRow.setPadding(0, 2, 0, 2); 

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

        column1.setTextColor(Color.BLACK); 
        column2.setTextColor(Color.BLACK); 
        column3.setTextColor(Color.BLACK); 
        column4.setTextColor(Color.BLACK); 
        column1.setSingleLine(false); 
        column2.setSingleLine(false); 
        column3.setSingleLine(false); 
        column4.setSingleLine(false); 

        if(row.getColumnOne() != null){ 
         column1.setText(row.getColumnOne()); 
        } 

        if(row.getColumnTwo() != null) { 
         column2.setText(row.getColumnTwo()); 
        } 

        if(row.getColumnThree() != null) { 
         column3.setText(row.getColumnThree()); 
        } 

        if(row.getColumnFour() != null) { 
         column4.setText(row.getColumnFour()); 
        } 

        //Now add a row 
        newRow.addView(column1); 
        newRow.addView(column2); 
        newRow.addView(column3); 
        newRow.addView(column4); 
        //Add row to table 
        table.addView(newRow, new TableLayout.LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.WRAP_CONTENT)); 
       } 
      } 

這裏是我的輸出:

enter image description here

需要

我需要的列之間的空格。我該怎麼做?

回答

5

設置填充左5DP爲每textveiw小部件一樣android:paddingLeft="5dp"

2

你必須爲列也增加paddind爲:

column1.setPadding(5, 5, 5, 5);  
column2.setPadding(5, 5, 5, 5);  
column3.setPadding(5, 5, 5, 5);  
column4.setPadding(5, 5, 5, 5); 

編輯: 然後你可以設置的LayoutParams如下:

column1.setLayoutParams(new LinearLayout.LayoutParams(60,90)); 
+0

但如何在自定義行的文本視圖中設置多行?因爲textview中的文本非常大,所以行在單行上變得很長。 –

+0

然後,你可以像set1.setLayoutParams(新的LinearLayout.LayoutParams(60,90))的TextView的setLayoutParams; –