删除除第一个之外的表行

时间:2014-04-24 06:19:48

标签: android

public void populate() {

        TableRow tr = new TableRow(getActivity());

        tr.setId(100 + count);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        Button b_add = new Button(getActivity());
        b_add.setId(20);
        b_add.setText("+");
        b_add.setTextColor(Color.RED);
        b_add.setGravity(Gravity.CENTER);
        b_add.setPadding(2, 2, 2, 2);
        b_add.setOnClickListener(add);

        tr.addView(b_add);

        TextView v_no = new EditText(getActivity());
        v_no.setId(200 + count);
        v_no.setHint("Vehicale NO");
        v_no.setTag("adress");
        v_no.setTag("v_no");
        v_no.setTextColor(Color.BLACK);
        v_no.setGravity(Gravity.CENTER);
        tr.addView(v_no);

        Button b_minus = new Button(getActivity());
        b_minus.setId(20);
        b_minus.setText("-");
        b_minus.setTextColor(Color.RED);
        b_minus.setGravity(Gravity.CENTER);
        b_minus.setPadding(2, 2, 2, 2);
        b_minus.setOnClickListener(remove);
        tr.addView(b_minus);
        // finally add this to the table row
        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        count++;
    }

我创建此代码以使用表格布局创建一个包含三列的表。它工作正常。但我需要删除除第一行之外的表格行。可以帮我这样做

1 个答案:

答案 0 :(得分:4)

while (yourTableLayout.getChildCount() > 1)
  yourTableLayout.removeView(yourTableLayout.getChildAt(yourTableLayout.getChildCount() - 1);

这应该遍历您的TableLayout并从底部到顶部逐行删除行,直到只剩下一行(不会被删除)。

相关问题