Adding rows to a TableLayout dynamically in Android
I am working on an application that needs to add rows to a table with 2 columns dynamically . Below is the code i am using .... i am getting a "ForceClose" error ... plss help ....
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_c开发者_运维百科ontent"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button android:id="@+id/b1" />
</TableRow>
</TableLayout>
this is my java code
TableLayout tb=(TableLayout)findViewById(R.id.table);
TableRow tr=new TableRow(getBaseContext());
Button b1=(Button)findViewById(R.id.b1);
LayoutParams lp=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
b1.setLayoutParams(lp);
b1.setText("I NEED HELP ! SOS ");
tr.addView(b1);
tb.addView(tr,lp);
tb.addView(tr,lp);
has a bug.. Lp is Layout Param which is already set to TableRow
so just use tb.addView(tr);
instead of tb.addView(tr,lp);
精彩评论