1

我有一个TableLayout

<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*" >

<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

    <TextView
         android:id="@+id/textView2"
        android:text="Day High"
        android:textStyle="bold" >
     </TextView>

    <TextView
        android:id="@+id/textView3"
         android:gravity="center_horizontal"
        android:text="28°F" >
     </TextView>

     <TextView
        android:id="@+id/textView4"
         android:gravity="center_horizontal"
        android:text="26°F" >
     </TextView>    

</TableRow>
</TableLayout>

还有一个Button

<Button
android:id="@+id/addItemTableRow"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="31dp"
android:textColor="@android:color/white"
android:text="Add row" />

我无法通过单击在button表格中添加新行来进行操作。

有什么想法我怎么能实现这个任务?

4

1 回答 1

1

在 Activity 类的 onCreate 方法上试试这个:

  //define a onClickListener for your button
    Button btnAddItem = (Button) findViewById(R.id.addItemTableRow);
    btnAddItem.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                            //create a new row to add
            TableRow row = new TableRow(YourActivity.this);
                            //add Layouts to your new row
            TextView txt = new TextView(YourActivity.this);
            txt.setText("New Row"); 
            row.addView(txt);
            //add your new row to the TableLayout:
            TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
            table.addView(row);

        }
    });
于 2013-10-16T18:24:15.023 回答