编程方式添加的TableRow到TableLayout不工作 [英] Programatically adding TableRow to TableLayout not working
问题描述
/ *查找Tablelayout main.xml中*定义/
TableLayout TL =(TableLayout)findViewById(R.id.SaleOrderLines);
/ *创建要添加一个新行。 * /
的TableRow TR =新的TableRow(本);
tr.setLayoutParams(新TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
/ *创建一个按钮是行的内容。 * /
按钮B =新的按钮(这一点);
b.setText(动态按钮);
b.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
/ *添加按钮来排。 * /
tr.addView(B);
/ *添加行TableLayout。 * /
//tr.setBackgroundResource(R.drawable.sf_gradient_03);
tl.addView(TR,新TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
不过,但没有被吸引到屏幕上。 (同为一体提到的<一个href="http://stackoverflow.com/questions/3200691/dynamically-add-tablerow-to-tablelayout">here)
当我加入 tr.setBackgroundResource(R.drawable.sf_gradient_03);
,与背景图像的行画,但没有按钮
下面是我的布局
&LT;! - 销售订单行开始 - &GT;
&LT;的LinearLayout机器人:ID =@ + ID / SaleOrderLinesArea机器人:方向=垂直
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
机器人:填充=10dip机器人:背景=@可绘制/ sf_gradient_11&GT;
&LT;的LinearLayout机器人:ID =@ + ID / subHeaderArea机器人:填充=10dip
机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
&GT;
&LT; TextView的机器人:ID =@ + ID / screenTitle
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=@色/ title_sub机器人:TEXTSTYLE =黑体
机器人:文本=订单行/&GT;
&LT; / LinearLayout中&GT;
&LT; TableLayout机器人:ID =@ + ID / SaleOrderLines
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
机器人:填充=10dip机器人:stretchColumns =*&GT;
&LT;的TableRow
机器人:背景=@可绘制/ sf_gradient_13机器人:填充=10dip
&GT;
&LT; TextView的机器人:ID =@ + ID / order_ref_label
机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=@色/ fg_prime机器人:文本=BLA BLA/&GT;
&LT; TextView的机器人:ID =@ + ID / product_label
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
机器人:TEXTSTYLE =黑体机器人:文字颜色=@色/ fg_title
机器人:文本=@字符串/产品/&GT;
&LT; TextView的Android的:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
机器人:TEXTSTYLE =黑体机器人:文字颜色=@色/ fg_title
机器人:文本=@字符串/ product_quantity/&GT;
&LT; /的TableRow&GT;
&LT;的TableRow机器人:背景=@可绘制/ sf_gradient_03
机器人:以下属性来=10dip机器人:paddingRight =10dip
&GT;
&LT; TextView的机器人:ID =@ + ID / order_ref_label
机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=@色/ fg_prime机器人:文本=Fooo/&GT;
&LT;微调机器人:ID =@ + ID / product_spinner
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
机器人:提示=@字符串/ customer_prompt&GT;
&LT; /微调&GT;
&LT; EditText上机器人:ID =@ + ID / product_uom_qty
机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
机器人:单线=真正的机器人:fadingEdge =横向/&GT;
&LT; /的TableRow&GT;
&LT; / TableLayout&GT;
&LT; LinearLayout中的android:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
机器人:paddingTop =5dip机器人:paddingBottom会=5dip&GT;
&LT;按钮机器人:ID =@ + ID / add_button机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT机器人:文本=添加行/&GT;
&LT; / LinearLayout中&GT;
&LT; / LinearLayout中&GT;
&LT;! - 销售订单行完 - &GT;
明白了,每个的LayoutParams
应 android.widget.TableRow的。的LayoutParams
一个提供给除 tl.addView(...)
/ *查找Tablelayout main.xml中*定义/
TableLayout TL =(TableLayout)findViewById(R.id.SaleOrderLines);
/ *创建要添加一个新行。 * /
的TableRow TR =新的TableRow(本);
tr.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
/ *创建一个按钮是行的内容。 * /
按钮B =新的按钮(这一点);
b.setText(动态按钮);
b.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
/ *添加按钮来排。 * /
tr.addView(B);
/ *添加行TableLayout。 * /
//tr.setBackgroundResource(R.drawable.sf_gradient_03);
tl.addView(TR,新TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
I'm trying to add table rows programatically 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 -->
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屋!