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

问题描述 投票:0回答:1

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

android tablelayout android-tablelayout tablerow
1个回答
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说使用RecyclerViewListViewTbaleLayout更好

© www.soinside.com 2019 - 2024. All rights reserved.