2011-09-02 81 views
36

我嘗試添加錶行程序代碼here編程方式添加到的TableRow不TableLayout工作

/* Find Tablelayout defined in main.xml */ 
    TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines); 
    /* Create a new row to be added. */ 
    TableRow tr = new TableRow(this); 
    tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    /* Create a Button to be the row-content. */ 
    Button b = new Button(this); 
    b.setText("Dynamic Button"); 
    b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    /* Add Button to row. */ 
    tr.addView(b); 
    /* Add row to TableLayout. */ 
    //tr.setBackgroundResource(R.drawable.sf_gradient_03); 
    tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

但但沒有下面是繪製到屏幕上。 (同一個提到here

當我加入tr.setBackgroundResource(R.drawable.sf_gradient_03);,與背景圖像的行畫,但沒有按鈕

這是我的佈局

<!-- Sale Order Lines START --> 
<LinearLayout android:id="@+id/SaleOrderLinesArea" android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:padding="10dip" android:background="@drawable/sf_gradient_11" > 
    <LinearLayout android:id="@+id/subHeaderArea" android:padding="10dip" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
     > 
     <TextView android:id="@+id/screenTitle" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:textColor="@color/title_sub" android:textStyle="bold" 
      android:text="Order Lines" /> 
    </LinearLayout> 
    <TableLayout android:id="@+id/SaleOrderLines" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:padding="10dip" android:stretchColumns="*"> 
     <TableRow 
      android:background="@drawable/sf_gradient_13" android:padding="10dip" 
      > 
      <TextView android:id="@+id/order_ref_label" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" 
       android:textColor="@color/fg_prime" android:text="bLA bLA" /> 
      <TextView android:id="@+id/product_label" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:textStyle="bold" android:textColor="@color/fg_title" 
       android:text="@string/product" /> 
      <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:textStyle="bold" android:textColor="@color/fg_title" 
       android:text="@string/product_quantity" /> 
     </TableRow> 
     <TableRow android:background="@drawable/sf_gradient_03" 
      android:paddingLeft="10dip" android:paddingRight="10dip" 
      > 
      <TextView android:id="@+id/order_ref_label" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" 
       android:textColor="@color/fg_prime" android:text="Fooo" /> 
      <Spinner android:id="@+id/product_spinner" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:prompt="@string/customer_prompt"> 
      </Spinner> 
      <EditText android:id="@+id/product_uom_qty" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:singleLine="true" android:fadingEdge="horizontal" /> 
     </TableRow> 
    </TableLayout> 
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:paddingTop="5dip" android:paddingBottom="5dip"> 
     <Button android:id="@+id/add_button" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="Add Lines" /> 
    </LinearLayout> 
</LinearLayout> 
<!-- Sale Order Lines END --> 
+0

你做tl.requestLayout();?在它添加行後? –

+0

@PMB未添加。 –

回答

116

明白了,每個LayoutParams應,除了一個android.widget.TableRow.LayoutParams使供給tl.addView(...)

/* Find Tablelayout defined in main.xml */ 
TableLayout tl = (TableLayout) findViewById(R.id.SaleOrderLines); 
/* Create a new row to be added. */ 
TableRow tr = new TableRow(this); 
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
/* Create a Button to be the row-content. */ 
Button b = new Button(this); 
b.setText("Dynamic Button"); 
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
/* Add Button to row. */ 
tr.addView(b); 
/* Add row to TableLayout. */ 
//tr.setBackgroundResource(R.drawable.sf_gradient_03); 
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); 
+3

謝謝米修....你救了我一天 –

+1

真的節省了很多時間和工作。 –

+1

omg。你是對的。 :D – gumuruh

6

請請注意,可能您的代碼是完美的,但您使用的課​​程並不合適。你可能已經使用import android.view.ViewGroup.LayoutParams,那裏的正確的類可以從下面的import語句:

進口android.widget.TableRow.LayoutParams

相關問題