Programmatically adding rows to TableLayout leads

2019-09-05 10:17发布

问题:

I'm trying to dynamically add rows to my table layout in Android (I referred to this tutorial: http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout). However, it's leading to an incorrect view hierarchy. I've attached the code as well as an output image.

XML File:

<TableLayout
    android:id="@+id/task_table"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*" >
    <TableRow
        android:id="@+id/task_heading_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/task_heading_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="Task"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="1"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="2"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="3"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="4"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="5"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="6"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="7"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
        <TextView
            android:id="@+id/time_8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:text="8"
            android:textSize="12sp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
    </TableRow>
</TableLayout>

Java Code:

 TableRow row = new TableRow(this);
 row.setLayoutParams(new TableRow.LayoutParams(
         TableRow.LayoutParams.MATCH_PARENT,
         TableRow.LayoutParams.WRAP_CONTENT)); 
 TextView t1 = new TextView(this);
 t1.setLayoutParams(new TableRow.LayoutParams(
         TableRow.LayoutParams.MATCH_PARENT,
         TableRow.LayoutParams.WRAP_CONTENT));

 String heading = "heading";
 t1.setText(heading);
 row.addView(t1);
 for (int i=0;i<8;i++){
     TextView view = new TextView(this);

     view.setLayoutParams(new TableRow.LayoutParams(
             TableRow.LayoutParams.MATCH_PARENT,
             TableRow.LayoutParams.WRAP_CONTENT));
     view.setText("child");
     row.addView(view);
 }
 task_table.addView(row, new TableLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

In the output all the 8 views with text "child" I'm adding in the loop get added to the column titled 1 instead of one "child" per column.

Expected output:

Task |     1 |     2 |     3 |      4 |     5 |    6 |     7 |     8 

heading | child | child | child | child | child | child | child | child

Actual output:

Task |     1 |     2 |     3 |      4 |     5 |    6 |     7 |     8 

heading | child child child child child child child child | | | | | | | 

Can someone please help me and let me know what I'm doing wrong? Any help will be greatly appreciated.

回答1:

Can someone please help me and let me know what I'm doing wrong?

I doubt that all of your 8 programmatically added TextViews go in the 1 column, most likely 6 will go in the heading column plus two in the 1 column.

In the layout you use the layout_span property for all the TextViews which means that each TextViews is set to spread over 6 normal columns. Now when you add those Textviews in code they get added filling one column at the time(no spreading) so in the end you'll not get a one to one match.

Remove the layout_span property from the layout TextViews or if, for some strange reason, you can't then add the same layout_span property to the TextViews that you add in code(through the TableRow.LayoutParams).

Also the you don't need to specify all of those layout_width/height parameters for the TableRow and its children as they have special constraints(so those values are useless). The android:gravity="center_horizontal" is again useless as you tell your TableRow to center its content when the TableLayout is set to stretch its content to fill the width.