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

时间:2018-01-01 23:37:03

标签: android tablelayout android-tablelayout tablerow

我有一个包含3列的表格布局,我需要最后一行有一列,当我添加它时,其他列的形式发生变化 enter image description here

我希望列“NIVEAU”位于中间

  

这是代码:

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一样添加最后一行但不改变外观吗?

1 个答案:

答案 0 :(得分:0)

我通过编程方式添加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