2016-11-16 25 views
0

我創建了一個自定義的TextView並且要動態添加到一個表格佈局:定製的TextView

custome TextView的代碼custom_table_cell命名爲:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/style_table_cell" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:text="Cell" > 

</TextView> 

我的活動代碼:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/bg_flight" > 

    <ScrollView android:layout_height="fill_parent" 
     android:layout_width="fill_parent" > 

     <TableLayout 
      android:id="@+id/tblInvoiceData" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 
     </TableLayout> 
    </ScrollView> 

</TableLayout> 

和我的代碼表行添加到表的佈局:

public TextView TableGetCell(Context context, String Text, int ColumnId) { 
     final TextView cell = (TextView) ((Activity) context).getLayoutInflater().inflate(R.layout.custom_table_cell, 
       null); 
     cell.setText(Text); 

     cell.setTypeface(Graphic.GetFontFaceFarsi(context)); 

     cell.setLayoutParams(new TableRow.LayoutParams(ColumnId)); 

     return cell; 
    } 

public void CreateRows() { 
     for (final Customer d : Customers) { 
      TableRow tr = new TableRow(this); 

      tr.addView(TableGetCell(this, String.valueOf(d.Name), 0)); 
      tr.addView(TableGetCell(this, String.valueOf(d.Family), 0)); 
      tr.addView(TableGetCell(this, String.valueOf(d.Age), 0)); 

      tblInvoiceData.addView(tr); 
     } 

    } 

和我的問題:爲什麼文本視圖不裹?

回答

0
TableRow row = new TableRow(this); 
TableRow.LayoutParams lp = new TableRow.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT); 
row.setLayoutParams(lp); 

TextView textView = new TextView(this); 
textView.setLayoutParams(new TableRow.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT, 1.0f)); 
textView.setText("yourValue"); 
row.addView(textView); 
tablelayout.addView(row); 
+0

謝謝you.you救了我 –