这是我要在其中连续添加textviews的n应用程序的代码
    动态地单击按钮却不起作用,您能告诉我代码有什么问题吗,我只是一个初学者。
    感谢你在期待。

public class MainActivity extends AppCompatActivity {

    Button save;
    TableLayout tableLayout;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button save = (Button) findViewById(R.id.save);

        TableLayout tableLayout = (TableLayout)          findViewById(R.id.table1);

        save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                TableRow tableRow;
                TextView textView;
                TableLayout tableLayout = (TableLayout)      findViewById(R.id.table1);

                for (int i = 0; i < 4; i++) {
                    tableRow = new TableRow(getApplicationContext());
                   for (int j = 0; j < 3; j++) {
                        textView = new     TextView(getApplicationContext());
                        textView.setText("@@@@");
                        textView.setPadding(20, 20, 20, 20);
                        tableRow.addView(textView);
                    }


                    tableLayout.addView(tableRow);
                }}});


                setContentView(tableLayout);






   }
}

and my xml layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kalla.monu.saplist.MainActivity">


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="28dp"
        android:layout_marginTop="160dp"
        android:id="@+id/table1"
        android:background="@color/colorPrimaryDark">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray"/>


           <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:backgroundTint="@android:color/holo_green_dark"     />

        <TableRow
               android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="save"
        android:id="@+id/save" />
     </RelativeLayout>

最佳答案

尝试这个,

我假设您正在尝试同时动态添加tablerows和textviews。因此,我从布局xml中删除了现有的tablerows。

TableLayout tableLayout = (TableLayout) findViewById(R.id.table1);

save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
    TableRow tableRow;
    TextView textView;
    for (int i = 0; i < 4; i++) {
    tableRow = new TableRow(this);
    for (int j = 0; j < 3; j++) {
        textView = new TextView(this);
        textView.setText("@@@@");
        textView.setPadding(20, 20, 20, 20);
        tableRow.addView(textView);
   }
   table1.addView(tableRow);
}}});


在XML中:

 <TableLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/table1"/>

10-08 06:58