如何在Android中的另一个tablelayout中添加tablelayout

时间:2018-05-20 20:40:01

标签: android tablelayout tablerow

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

enter image description here

您可以帮我在Android Studio中执行此操作。

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

这里是Test2.java类

{

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);
    }
}

}

1 个答案:

答案 0 :(得分:0)

试试这个代码段

 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);

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