1

我有这三个具有相同数量元素的列表

List<String>competitoinsIDs = new LinkedList<String>();
List<String>marks = new LinkedList<String>();
List<String>numOfQuestions = new LinkedList<String>();

我想将每个列表的第一个元素放在一个 tablerow 中,然后将每个列表的第二个元素放在另一个 tablerow 中,请你帮帮我,这是 xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableLayout
        android:id="@+id/tlMarksTable"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dip"
            android:weightSum="2" >

            <TextView
                android:id="@+id/tvMarksCompetitionID"
                android:layout_weight="1"
                android:text="Competition"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tvMarksMarks"
                android:layout_weight="1"
                android:text="Marks"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tvMarksQuestionsNum"
                android:layout_weight="1"
                android:text="Questions"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>
    </TableLayout>

</FrameLayout>
4

2 回答 2

5

首先将您的 XML 文件更改为:

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableLayout
        android:id="@+id/tlMarksTable"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    </TableLayout>
    </FrameLayout>

我们将动态添加所有内容。

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    List<String>competitoinsIDs = new LinkedList<String>();
    List<String>marks = new LinkedList<String>();
    List<String>numOfQuestions = new LinkedList<String>();

 //make sure that the lists contain data or else display will be blank screen

    TableRow.LayoutParams  params1=new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
    TableRow.LayoutParams params2=new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    TableLayout tbl=(TableLayout) findViewById(R.id.tlMarksTable);
    for(int ctr=0;ctr<marks.size();ctr++)
    {
    //Creating new tablerows and textviews
    TableRow row=new TableRow(this);
    TextView txt1=new TextView(this);
    TextView txt2=new TextView(this);
    TextView txt3=new TextView(this);
    //setting the text
    txt1.setText(competitoinsIDs.get(ctr));
    txt2.setText(marks.get(ctr));
    txt3.setText(numOfQuestions.get(ctr));
    txt1.setLayoutParams(params1);
    txt2.setLayoutParams(params1);
    txt3.setLayoutParams(params1);
    //the textviews have to be added to the row created
    row.addView(txt1);
    row.addView(txt2);
    row.addView(txt3);
    row.setLayoutParams(params2);
    tbl.addView(row);
    }

}
于 2012-07-05T11:43:45.943 回答
1

请在此处查看 android 代码并在您的活动onCreate方法中使用它

setContentView(R.layout.table_layout_xml); // use your layout xml file here

TableLayout tableLayout = (TableLayout)findViewById(R.id.tlMarksTable);

List<String>competitoinsIDs = new LinkedList<String>();
List<String>marks = new LinkedList<String>();
List<String>numOfQuestions = new LinkedList<String>();


// adding static values to test the layout. use your dynamic data here
competitoinsIDs.add("123");
competitoinsIDs.add("124");
competitoinsIDs.add("125");

marks.add("56");
marks.add("57");
marks.add("58");

numOfQuestions.add("10");
numOfQuestions.add("11");
numOfQuestions.add("12");

TableRow tableRows[] = new TableRow[competitoinsIDs.size()];
for (int i = 0; i < tableRows.length; i++) {

    tableRows[i] = new TableRow(this);
    tableRows[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    tableRows[i].setWeightSum(2.0f);
    tableRows[i].setPadding(5, 5, 5, 5);

    TextView text = new TextView(this);
    text.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
            android.widget.TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
    text.setText(competitoinsIDs.get(i));
    tableRows[i].addView(text);

    text = new TextView(this);
    text.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
            android.widget.TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
    text.setText(marks.get(i));
    tableRows[i].addView(text);

    text = new TextView(this);
    text.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
            android.widget.TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
    text.setText(numOfQuestions.get(i));
    tableRows[i].addView(text);

    tableLayout.addView(tableRows[i]);

}
于 2012-07-05T11:42:27.967 回答