2012-06-27 31 views
1

我想此表來儘可能延伸以填充包含線性佈局的剩餘的(空的)區域它表不能填滿含佈局的寬度

enter image description here

我使用生成表以下方法

public void drawTable(LinearLayout tableLayout,String tableTitle, String[][] data,String[] headerTitles, int type){ 

     //clearing previous views 
     tableLayout.setVisibility(View.VISIBLE); 
     tableLayout.setGravity(Gravity.CENTER); 
     int chids = tableLayout.getChildCount(); 
     if (chids > 0){ 
      tableLayout.removeAllViews(); 
     } 

     if((headerTitles!= null) && (headerTitles.length != data[0].length)){ 
      return; 
     } 

     TableLayout table = new TableLayout(context); 
//  table.setStretchAllColumns(true); 
//  table.setShrinkAllColumns(true); 


     //Rendering the table title 
     TextView tableTitleView = new TextView(context); 
     tableTitleView.setText(tableTitle); 
     tableTitleView.setTextColor(Color.WHITE); 
     tableTitleView.setGravity(Gravity.CENTER); 
     tableTitleView.setTextSize(18); 
     tableTitleView.setTextAppearance(context, R.style.boldText); 
     tableTitleView.setBackgroundColor(Color.parseColor("#008888")); 
     tableTitleView.setPadding(2, 2, 2, 10); 
     tableLayout.addView(tableTitleView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 








     ScrollView scroll1 = new ScrollView(context); 
     HorizontalScrollView scroll2 = new HorizontalScrollView(context); 


     int rows = data.length; 
     int columns = data[0].length; 

     if(headerTitles!=null){ 
      //Rendering the header 
      TableRow headerRow = new TableRow(context); 
      for (int i = 0; i < columns; i++) { 
       TextView t = new TextView(context); 
       t.setTextSize(17); 
       t.setTextAppearance(context, R.style.boldText); 
       t.setPadding(5, 5, 5, 5); 
       t.setText(headerTitles[i]); 
       t.setGravity(Gravity.CENTER); 
       t.setTextColor(Color.WHITE); 
       t.setBackgroundResource(R.drawable.text_border_header_1); 
       headerRow.addView(t, LinearLayout.LayoutParams.WRAP_CONTENT, 
         LinearLayout.LayoutParams.WRAP_CONTENT); 
      } 
      table.addView(headerRow, new TableLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     } 

     //Rendering the Table Data 
     TableRow row; 
     for (int current = 0; current < rows ; current++) { 
      row = new TableRow(context); 
      TextView t; 
      for(int i =0 ; i < columns ; i++){ 
       t = new TextView(context); 
       t.setTextSize(15); 
       t.setPadding(5,5,5,5); 
       t.setText(data[current][i]); 
       t.setGravity(Gravity.CENTER); 

       if (type == 1) { 
        if (current % 2 == 0) { 
         t.setBackgroundResource(R.drawable.text_border_odd); 
        } else { 
         t.setBackgroundResource(R.drawable.text_border_even); 
        } 
       }else if (type == 2) { 
        if (current % 2 == 0) { 
         t.setBackgroundResource(R.drawable.text_border_odd_2); 
        } else { 
         t.setBackgroundResource(R.drawable.text_border_even_2); 
        } 
       }else if (type == 3) { 
        if (current % 2 == 0) { 
         t.setBackgroundResource(R.drawable.text_border_odd_3); 
        } else { 
         t.setBackgroundResource(R.drawable.text_border_even_3); 
        } 
       } 

       t.setTextColor(Color.BLACK); 
       row.addView(t,TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT); 
      } 

      table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

     } 
     scroll2.addView(table,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); 
     scroll1.addView(scroll2,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); 
     scroll1.setPadding(2, 20, 2, 20); 

     tableLayout.addView(scroll1,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); 


    } 

我找不出準確的問題與我的代碼。

編輯:

當我直接加入該表到線性佈局而不是滾動觀看錶與線性佈局寬度相符。看來我的問題是滾動視圖

+0

嘗試tableLayout.setStretchAllColumns(true).. –

+0

@Dheeresh我試過了。仍然是相同的問題 – Abdelwahed

+0

這一行可能是你的問題:「table.addView(headerRow,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));」而改爲改變寬度fill_parent以及? – Guardanis

回答

1

我找到了答案Here(感謝菲利克斯)

只是一個單一的代碼行被添加到滾動views

scrollView.setFillViewPort(true); 
0

在你的TableLayout對象上試試這個。

TableLayout.LayoutParams params = new TableLayout.LayoutParams(); 
params.width = LayoutParams.FILL_PARENT; 
table.setLAyoutParams(params); 

編輯:

TextView tView = new TextView(this); 
LinearLayout.LayoutParams params = tView.getLayoutParams(); 
params.width = LayoutParams.FILL_PARENT; 
params.weight = 1; 
tView.setLayoutParams(params); 

問候, Aqif

+0

仍然不能正常工作 – Abdelwahed

+0

檢查我的編輯!爲每個添加到TableRows或Headers的TextVIew添加此項。 編輯: 重量參數將是行項之間屏幕的平等分配的關鍵。 –

+0

我將代碼中的所有佈局參數更改爲FILL_PARENT並添加了代碼,但完全沒有任何更改。桌子的外觀還是一樣。 – Abdelwahed