【问题标题】:How can I add a tablelayout into another tablelayout in Android如何在 Android 中将一个表格布局添加到另一个表格布局中
【发布时间】:2018-05-20 20:40:01
【问题描述】:

是否可以在 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);
    }
}

}

【问题讨论】:

  • 为什么不在展开式列表视图中使用表格布局?
  • 兄弟请看图,我觉得ListView也可以,但是实现起来会比较困难
  • “我想在特定位置添加新行”这才是真正的要求,对吧?
  • 使用 addview 插入特定索引
  • 你是对的。你能推荐任何关于它的教程或例子吗?

标签: android tablelayout tablerow


【解决方案1】:

试试这个sn-p

 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)因为您想在该视图下方(旁边)添加

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多