以编程方式向TableLayout添加行会导致视图层次结构不正确

时间:2013-03-23 18:26:03

标签: android android-layout android-tablelayout

我试图在Android中的表格布局中动态添加行(我在本教程中提到:http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout)。但是,它会导致视图层次结构不正确。我已经附加了代码和输出图像。

XML文件:

<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代码:

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

在输出中所有8个视图都带有文本&#34; child&#34;我在循环中添加添加到标题为1的列而不是一个&#34; child&#34;每列。

预期产出:

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

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

实际输出:

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

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

有人可以帮助我,让我知道我做错了什么吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

  

有人可以帮助我,让我知道我做错了什么吗?

我怀疑您的所有8个以编程方式添加的TextViews都在1列中,很可能6个会在标题列中加上1个列中的两个。

在布局中,您对所有layout_span使用TextViews属性,这意味着每个TextViews设置为分布在6个普通列上。现在当你在代码中添加Textviews时,他们会添加一个列(没有扩散),所以最后你不会得到一对一的匹配。

从布局layout_span中移除TextViews属性,或者由于某些奇怪的原因,您无法将layout_span属性添加到TextViews的{​​{1}}添加代码(通过TableRow.LayoutParams)。

此外,您不需要为TableRow及其子项指定所有这些layout_width / height参数,因为它们具有特殊约束(因此这些值无用)。当android:gravity="center_horizontal"被设置为拉伸其内容以填充宽度时,TableRow告诉您TableLayout将其内容置于中心位置时,{{1}}再次无效。

相关问题