如何在不更改其他行的情况下将 TableRow 添加到 TableLayout

How to add a TableRow to TableLayout without making changes to other Rows

我有一个包含 3 列的 table 布局,我需要让最后一行包含一列,当我添加它时,其他列的形式会发生变化

我希望列 "NIVEAU" 位于中间

here is the code :

View v = new View(contentView.getContext());
        v.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                2
        ));
        v.setBackgroundColor(Color.BLACK);

        t1 = (TableLayout) contentView.findViewById(R.id.main_table);

        TableRow tr_head = new TableRow(contentView.getContext());
        tr_head.setId(10);
        tr_head.setGravity(Gravity.CENTER);

        tr_head.setBackgroundColor(Color.GRAY);
        tr_head.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        TextView label_matiere = new TextView(contentView.getContext());
        label_matiere.setId(20);
        label_matiere.setText("MATIERE");
        label_matiere.setMaxWidth(70);
        label_matiere.setTextColor(Color.WHITE);
        label_matiere.setPadding(5, 5, 5, 5);
        tr_head.addView(label_matiere);// add the column to the table row here

        TextView label_niveau = new TextView(contentView.getContext());
        label_niveau.setId(21);
        label_niveau.setText("NIVEAU"); 
        label_niveau.setTextColor(Color.WHITE); 
        label_niveau.setPadding(5, 5, 5, 5);
        tr_head.addView(label_niveau); 

        TextView label_prix = new TextView(contentView.getContext());
        label_prix.setId(22);
        label_prix.setText("PRIX(DH)"); 
        label_prix.setTextColor(Color.WHITE); 
        label_prix.setPadding(5, 5, 5, 5); 
        tr_head.addView(label_prix);     

        t1.addView(tr_head, new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT));
        t1.addView(v);


        Integer count=0;

        while (count<=7) {
            String Matiere = "matiere :"+count;
            String niveau = "niveau :"+count;
            Integer Prix = count+120;


            TableRow tr = new TableRow(contentView.getContext());
            tr.setId(100+count);
            tr.setPadding(5, 5, 5, 5);
            tr.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT));



  TextView labelmatiere = new TextView(contentView.getContext());
            labelmatiere.setId(200+count);
            labelmatiere.setText(Matiere);
            labelmatiere.setPadding(2, 0, 5, 0);
            tr.addView(labelmatiere);
  TextView labelniveau = new TextView(contentView.getContext());
            labelniveau.setId(200+count);
            labelniveau.setText(niveau);
            tr.addView(labelniveau);
  TextView labelprix = new TextView(contentView.getContext());
            labelprix.setId(200+count);
            labelprix.setText(Prix.toString());
            tr.addView(labelprix);


            t1.addView(tr, new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));

            View l = new View(contentView.getContext());
            l.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    1
            ));
            l.setBackgroundColor(Color.BLACK);
            t1.addView(l);
            count++;
        }
        TableRow add = new TableRow(contentView.getContext());
        add.setId(100+count);
        add.setPadding(5, 5, 5, 5);
        add.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        TextView addMatiere = new TextView(contentView.getContext());
        addMatiere.setId(200+count);
        addMatiere.setText("+Ajouter une nouvelle matiere");
        addMatiere.setTextColor(Color.BLACK);
        addMatiere.setGravity(Gravity.CENTER);
        add.setGravity(Gravity.CENTER);
        add.addView(addMatiere);

        t1.addView(add, new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT));

请问这个问题有什么解决办法 我可以像添加 TextView 一样添加最后一行但不改变外观吗?

好吧,我通过像这样以编程方式添加 span 解决了这个问题:

TextView addMatiere = new TextView(contentView.getContext());
        addMatiere.setId(200+count);
        addMatiere.setText("+Ajouter une nouvelle matiere");
        addMatiere.setTextColor(Color.BLACK);
        addMatiere.setPadding(2, 0, 5, 0);
        addMatiere.setGravity(Gravity.CENTER);

        add.setGravity(Gravity.CENTER);
        add.addView(addMatiere);
        TableRow.LayoutParams params = (TableRow.LayoutParams) addMatiere.getLayoutParams();
        params.span=3; //amount of columns you will span
        addMatiere.setLayoutParams(params);

正如#Barns 所说,使用 RecyclerViewListView 优于 TbaleLayout