如何在 Android 中将一个表格布局添加到另一个表格布局中

How can I add a tablelayout into another tablelayout in Android

是否可以在 Android Studio 中将一个表格布局添加到另一个表格布局中? 实际上,我想在特定位置添加一个新行。但是当我尝试添加一个新行时,它总是被添加到所有行的下方。

你能帮我在 Android Studio 中做这个吗?

注意:它将在运行时运行,我的意思是完全 Java 代码。

这里Test2.javaclass

{

int col = 4, row = 3;
TableLayout tableLayout;
EditText editText;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test2);


    tableLayout = (TableLayout) findViewById(R.id.table2);
    for (int i = 1; i <= col; i++) {
        final TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
        final TextView text = new TextView(this);
        if (i == 1) {
            text.setText(i + "st");
        } else if (i == 2) {
            text.setText(i + "nd");
        } else if (i == 3) {
            text.setText(i + "rd");
        } else {
            text.setText(i + "th");
        }
        text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        text.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        text.setTextColor(-16777216);
        tableRow.addView(text);
        for (int j = 1; j <= row; j++) {
            editText = new EditText(this);
            editText.setTag(editText + String.valueOf(j));
            editText.setHint("Input " + j);
            editText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            editText.setBackground(null);
            tableRow.addView(editText);
        }
        final Button button = new Button(this);
        button.setText("Add");
        button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TableRow r = new TableRow(Test2.this);

                for (int j = 1; j <= row; j++) {
                    editText = new EditText(Test2.this);
                    editText.setTag(editText + String.valueOf(j));
                    editText.setHint("New " + j);
                    editText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                    editText.setBackground(null);
                    r.addView(editText);
                }
                tableLayout.addView(r);
            }
        });
        tableRow.addView(button);
        tableLayout.addView(tableRow);
    }
}

}

试试这个片段

 int index = tableLayout.getChildCount();
        tableRow.setTag(index);

        final Button button = new Button(this);
        button.setText("Add");
        button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TableRow r = new TableRow(MainActivity.this);

                for (int j = 1; j <= row; j++) {
                    editText = new EditText(MainActivity.this);
                    editText.setTag(editText + String.valueOf(j));
                    editText.setHint("New " + j);
                    editText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                    editText.setBackground(null);
                    r.addView(editText);
                }


                int index = (Integer)tableRow.getTag();
                System.out.println(index);
                tableLayout.addView(r,(index+1));

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

为等于索引位置的每个表行设置一个标记。然后使用该标记在该索引处添加一个新视图。 (tag+1) 因为您想在该视图下方(旁边)添加