【发布时间】: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 以填写序列号。其他文本视图是从布局动态生成的,并添加到此表格布局中。我不知道我这样做是否正确。