以编程方式将 TableRow 添加到 TableLayout 不起作用

发布于 2024-12-02 17:37:59 字数 4293 浏览 0 评论 0原文

我尝试按照代码此处以编程方式添加表格行

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

I'm trying to add table rows programmatically following the code here

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

But but nothing is drawn to the screen. (same as one mentioned here)

When i added tr.setBackgroundResource(R.drawable.sf_gradient_03);, the row with the background image is drawn but not the button

Here is my Layout

<!-- 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 -->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛上残留的泪 2024-12-09 17:37:59

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

Got it, every LayoutParams should be of android.widget.TableRow.LayoutParams except one that supplied to 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));
-残月青衣踏尘吟 2024-12-09 17:37:59

请注意,可能您的代码是完美的,但您使用的类不正确。您可能使用过 import android.view.ViewGroup.LayoutParams ,其中可以从以下 import 语句获取正确的类:

导入android.widget.TableRow.LayoutParams

Please do note that, may be your code is perfect but the classes you are using are not proper. You may have used import android.view.ViewGroup.LayoutParams , where as the correct class is available from following import statement :

import android.widget.TableRow.LayoutParams

温暖的光 2024-12-09 17:37:59

就我而言,我将宽度从 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));

In my case, I changed the width from TableRow.LayoutParams.WRAP_CONTENT to TableRow.LayoutParams.MATCH_PARENT, then it works.

TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文