android - 难以显示动态添加的表行
问题描述
我正在一个基本上以网格格式显示问题和答案的屏幕上工作,并且我将它们全部动态地放入表格行中。我遇到的问题是,一旦我放入第一行(标题),表格中的其余行就不会显示出来。我首先添加标题。这出现了。
protected void setupTableHeaders() {
TableRow tableRow = getNewTableRow();
tableRow.setTag(header);
TextView textView = new TextView(this);
GlobalVars.setupText(this,textView,32320); //Event
textView.setTextColor(R.color.black);
LayoutParams params = new LayoutParams(0,LayoutParams.WRAP_CONTENT,3.0f);
params.setMarginStart(50);
tableRow.addView(textView,params);
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
Iterator it = mapQuestions.entrySet().iterator();
//first get all the answers
if (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
HolderQuestion holderQuestion = mapQuestions.get(pair.getKey());
//assume all answers sets for all questions on this screen are the same.
int i = 0;
for (HolderAnswer answer : holderQuestion.getListAnswers()) {
TextView textViewHeader = new TextView(this);
textViewHeader.setTextColor(R.color.black);
GlobalVars.setupText(this, textViewHeader, answer.getTextID());
tableRow.addView(textViewHeader, (new LayoutParams(0, LayoutParams.WRAP_CONTENT, .5f)));
if (i < holderQuestion.getListAnswers().size() - 1) {
tableRow.addView(getNewDivider(), (new LayoutParams(0, LayoutParams.WRAP_CONTENT, .1f)));
}
i++;
}
}
table1.addView(tableRow);
}
然后我添加了未显示的问题和答案:
protected void setupRadioButtonAnswers() {
for (int i=0;i<holderQuestionMultis.size();i++) { //assume this is populated correctly
TableRow tableRow = getNewTableRow();
tableRow.setTag(answerRow);
TextView textView = new TextView(this);
textView.setTextColor(R.color.black);
GlobalVars.setupText(this,textView,holderQuestionMultis.get(i).getHolderQuestion().getTextID());
textView.setTag(textCell);
textView.setVisibility(View.VISIBLE);
LayoutParams params = new LayoutParams(0,LayoutParams.WRAP_CONTENT,3.0f);
params.leftMargin = 50;
tableRow.addView(textView,params);
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
for (int j=0;j<holderQuestionMultis.get(i).getRadioButtons().size();j++) {
RadioButton radioButton = holderQuestionMultis.get(i).getRadioButtons().get(j);
radioButton.setVisibility(View.VISIBLE);
tableRow.addView(radioButton,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.5f)));
if (j< holderQuestionMultis.size()-1) {
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
}
}
tableRow.setVisibility(View.VISIBLE);
table1.addView(tableRow);
}
}
以及获取单选按钮和表格行的两种方法:
// table elements
protected TableRow getNewTableRow() {
TableRow tableRow = new TableRow(this);
TableLayout.LayoutParams tlparams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
tableRow.setBackground(getResources().getDrawable(R.drawable.border_table_multi_grid));
tableRow.setLayoutParams(tlparams);
return tableRow;
}
protected RadioButton getRadioButton() {
RadioButton radioButton = new RadioButton(this);
radioButton.setBackground(getResources().getDrawable(R.drawable.radio_button_multigrid));
//radioButton.setButtonDrawable(R.color.transparent);
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
radioButton.setLayoutParams(params);
return radioButton;
}
表格布局xml:
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal"
android:layout_above="@+id/viewSpacer"
android:layout_below="@+id/lblSubQuestionText"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:background="@drawable/border_table_multi_grid">
我错过了什么?第一个表格标题行是否覆盖了其他行?为什么他们不出现?
解决方案
事实证明,这段代码:
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
是添加包含视图的 LinearLayout 的罪魁祸首。仅添加视图即可解决问题。
推荐阅读
- rust - 在迭代器特征中指定关联类型的生命周期
- c# - 如何对依赖的 cpde 进行单元测试
- postgresql - 在 PostGIS / PostgreSQL 中使用 st_transform 从质心坐标创建新列
- scala - 如何在 Http4s 中添加自定义错误响应?
- c# - 如何在 Windows 窗体 c# 中限制 DatetimePicker?
- amazon-web-services - 在 aws lambda 中输出 http 请求 node.js
- c++ - 字符串变成一个链表然后检查括号平衡
- python - 我们什么时候需要socket,什么时候需要request?
- r - 使用计算的逻辑回归模型,根据 R 中合适的截止值创建分类器
- git - 上游合并的Git合并策略-如果没有我们的更改,如何应用`他们的`