以编程方式向TableLayout添加TableRow无法正常工作

50

我正试图通过这里的代码 动态添加表格行

来编写程序。

    /* 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));

但是屏幕上没有任何显示。(与此处 提到的问题 相同)

当我添加了 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 -->

你在添加行后是否执行了tl.requestLayout();? - Pooja M. Bohora
3个回答

150

明白了,每个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));

1
谢谢!它起作用了。但我不理解“除了提供给tl.addView()的一个” ,因为你在那里也使用了它。 - PhilLab
这似乎是为了向网格添加一行而进行的大量工作。新构造函数被调用了4次。 - Paul McCarthy
1
@EricBarr,那个链接现在重定向到垃圾邮件了。 - m.qayyum
如果tablerow已经包含多个元素...我们是否可以直接将tablerow复制到单个变量中,并对现有元素进行操作? - gumuruh

10
请注意,您的代码可能是完美的,但您使用的类可能不正确。您可能已经使用了import android.view.ViewGroup.LayoutParams,而正确的类可以从以下导入语句中找到:

import android.widget.TableRow.LayoutParams


0
在我的情况下,我将宽度从TableRow.LayoutParams.WRAP_CONTENT更改为TableRow.LayoutParams.MATCH_PARENT,然后它就可以工作了。
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接