【问题标题】:Dynamically adding columns to a tablerow. Adding textviews in a loop. settext not working动态地将列添加到 tablerow。在循环中添加文本视图。设置文本不起作用
【发布时间】:2023-03-18 23:18:01
【问题描述】:

我有一个 9 行的表格布局。第一个是标题。但列数不同。所以我需要将 textview 动态添加到表格行中。 我正在膨胀表格行并为固定的第一列添加文本。其他 coulmns 我正在尝试将其添加到循环中。但我只得到第一个单元格。

我的预期输出是这样的:

col#   A   B   C   D  E  F
----------------------------
1      65  6   6   5  6  7
----------------------------
2      6  4   7   4  6  8

列数可以是 3 到 6。但行数是 8

我得到一个这样的输出:

 col#   
    ----------------------------
    1   
    ----------------------------
    2   

我正在尝试显示第一行。但我只得到第一个单元格。有人可以帮忙吗?

我的带有表格行的 xml

<?xml version="1.0" encoding="utf-8"?>

  <TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
        <TextView android:id="@+id/sn4"
                    android:textSize="25sp"
                    android:layout_marginTop="15dip"
                    android:layout_marginLeft="20dip"
                    style="@style/normalText"
                    android:textColor="@color/title"
             />



</TableRow>

我有另一个用于 textview 的 xml

<?xml version="1.0" encoding="utf-8"?>


    <TextView   xmlns:android="http://schemas.android.com/apk/res/android" 
                android:id="@+id/txtfactor"
                android:textSize="25sp"
                android:layout_marginTop="15dip"
                android:layout_marginLeft="20dip"
                style="@style/normalText"
                android:textColor="@color/title"
         />

在我的代码中:

tbl = (TableLayout)findViewById(R.id.tbltrail); for(int i=0; i

            for(int j=0; j<count; j++){
                FactorObj factorObj = factorList.get(i);
                View row = (View) getLayoutInflater().inflate(R.layout.trial_row_content, null);
                TextView factorname = (TextView)row.findViewById(R.id.txtfactor);
                factorname.setId(200+j);
                Log.v(null, "factor name "+factorObj.getFactorName());
                factorname.setText("sdf");
                child.addView(row);



            }
            tbl.addView(child);


        }else{
            sno.setText(String.valueOf(i));
            for(int j=0; j<count; j++){
                FactorObj factorObj = factorList.get(i);
                View row = (View) 

                getLayoutInflater().inflate(R.layout.trial_row_content, null);
                TextView factorname = (TextView)row.findViewById(R.id.txtfactor);
                factorname.setId(100+j);
                factorname.setText("ss");
                child.addView(row);


            }
            tbl.addView(child);
        }

    }

【问题讨论】:

  • addView 重新定义视图?从未使用过“行”。相反,它的一部分被添加到容器中。 IMO 不干净。
  • 对不起,我已经编辑了代码。在每个表格行中,我都保留了一个固定的 textview 以填写序列号。其他文本视图是从布局动态生成的,并添加到此表格布局中。我不知道我这样做是否正确。

标签: android android-layout


【解决方案1】:

为此创建一个视图组的行

mCell = new CellViewGroup(this,parameters);

和 参考xml中的表格布局

 mTableLayout = (TableLayout) findViewById(R.id.mytable);

动态创建表格行

TableRow mRow = new TableRow(this);

将视图组添加到行

mRow.addView(mCell);

将其添加到表格布局中

mTableLayout.addView(mRow);

如果您不了解视图组,只需 google 一下.....

【讨论】:

  • 我已经为不同的应用程序尝试过这个。这里我试图从 xml 中膨胀 tablerow。在循环中显示时。 setText 不工作。无论如何,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多