繁体   English   中英

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

[英]How can I add a tablelayout into another tablelayout in Android

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

在此处输入图片说明

您能帮我在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);
    }
}

}

试试这个片段

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM