以编程方式将TableRow添加到TableLayout不起作用 [英] Programmatically adding TableRow to TableLayout not working

查看:64
本文介绍了以编程方式将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));

但是,屏幕上没有任何内容. (与在此处提到的人一样)

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

当我添加tr.setBackgroundResource(R.drawable.sf_gradient_03);时,会绘制带有背景图像的行,但不会绘制按钮

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

这是我的布局

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

推荐答案

知道了,每个LayoutParams应该属于android.widget.TableRow.LayoutParams,但提供给tl.addView(...)

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

这篇关于以编程方式将TableRow添加到TableLayout不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆