难以动态添加表行以显示

时间:2018-06-11 15:46:42

标签: android tablelayout tablerow

我正在制作一个基本上以网格格式显示问题和答案的屏幕,我将它们全部放入表格行中。我遇到的问题是,一旦我放入第一行(标题),表中的其余行就不会显示出来。我先添加标题。这表明了。

 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);
    }

}

获取radioButtons和table行的两种方法:

// 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;
}

tablelayout 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">

我错过了什么?第一个表标题行是否覆盖了其他行?他们为什么不出现?

1 个答案:

答案 0 :(得分:0)

原来是这样的代码:

tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));

罪魁祸首是添加包含视图的LinearLayout。仅添加视图即可解决问题。