编程加入行TableLayout导致不正确的视图层级 [英] Programmatically adding rows to TableLayout leads to incorrect view hierarchy

查看:181
本文介绍了编程加入行TableLayout导致不正确的视图层级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态行添加到Android中我的表布局(我提到这个教程: HTTP ://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout )。然而,它的导致一个不正确的视图层级。我已经把它贴在code以及输出图像。

XML文件:

 < TableLayout
    机器人:ID =@ + ID / task_table
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:shrinkColumns =*
    机器人:stretchColumns =*>
    <的TableRow
        机器人:ID =@ + ID / task_heading_row
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=CENTER_HORIZONTAL>        <的TextView
            机器人:ID =@ + ID / task_heading_view
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_span =6
            机器人:文字=任务
            机器人:TEXTSIZE =12SP
            机器人:文字样式=大胆
            机器人:字体=衬>
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / TIME_1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_span =6
            机器人:文字=1
            机器人:TEXTSIZE =12SP
            机器人:文字样式=大胆
            机器人:字体=衬>
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / time_2
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_span =6
            机器人:文字=2
            机器人:TEXTSIZE =12SP
            机器人:文字样式=大胆
            机器人:字体=衬>
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / time_3
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_span =6
            机器人:文字=3
            机器人:TEXTSIZE =12SP
            机器人:文字样式=大胆
            机器人:字体=衬>
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / time_4
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_span =6
            机器人:文字=4
            机器人:TEXTSIZE =12SP
            机器人:文字样式=大胆
            机器人:字体=衬>
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / time_5
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_span =6
            机器人:文字=5
            机器人:TEXTSIZE =12SP
            机器人:文字样式=大胆
            机器人:字体=衬>
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / time_6
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_span =6
            机器人:文字=6
            机器人:TEXTSIZE =12SP
            机器人:文字样式=大胆
            机器人:字体=衬>
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / time_7
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_span =6
            机器人:文字=7
            机器人:TEXTSIZE =12SP
            机器人:文字样式=大胆
            机器人:字体=衬>
        < / TextView的>
        <的TextView
            机器人:ID =@ + ID / time_8
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_span =6
            机器人:文字=8
            机器人:TEXTSIZE =12SP
            机器人:文字样式=大胆
            机器人:字体=衬>
        < / TextView的>
    < /&的TableRow GT;
< / TableLayout>

Java的code:

 的TableRow行=新的TableRow(本);
 row.setLayoutParams(新TableRow.LayoutParams(
         TableRow.LayoutParams.MATCH_PARENT,
         TableRow.LayoutParams.WRAP_CONTENT));
 TextView的T1 =新的TextView(本);
 t1.setLayoutParams(新TableRow.LayoutParams(
         TableRow.LayoutParams.MATCH_PARENT,
         TableRow.LayoutParams.WRAP_CONTENT)); 字符串标题=标题;
 t1.setText(航向);
 row.addView(T1);
 的for(int i = 0; I< 8;我++){
     TextView的视图=新的TextView(本);     view.setLayoutParams(新TableRow.LayoutParams(
             TableRow.LayoutParams.MATCH_PARENT,
             TableRow.LayoutParams.WRAP_CONTENT));
     view.setText(子);
     row.addView(视图);
 }
 task_table.addView(行,新TableLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));

在输出所有与文本子我添加循环中的8次被添加到标题的1而不是一个子每列的列

期望的输出:

 任务| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8标题|孩子|孩子|孩子|孩子|孩子|孩子|孩子|儿童


实际输出:

 任务| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8标题|孩童孩童孩童孩童| | | | | | |

能否有人请帮助我,让我知道我做错了吗?任何帮助将大大AP preciated。


解决方案

  

能否有人请帮助我,让我知道我做错了吗?


我怀疑所有的8编程方式添加在 1 TextViews 去,最有可能的6会去在1列的标题栏加上两个。

在使用 layout_span 属性的布局所有的 TextViews 这意味着每个 TextViews 超过6列的正常设置为s $ p $垫。现在,当你添加的 Textviews 在code,他们被添加在时间(编号S preading)灌装一列所以最后你会得到不一对一的匹配

从布局中删除 layout_span 属性 TextViews 或者,出于某种奇怪的原因,你不能再添加相同 layout_span 属性设置为 TextViews 您在code添加(通过 TableRow.LayoutParams )。

另外,你并不需要指定所有这些layout_width / height参数为的TableRow 及其子女,因为他们有特殊的限制(所以这些值是没用的) 。在安卓重力=CENTER_HORIZONTAL是你告诉再次没用你的的TableRow 来中心的内容时, TableLayout 设置为舒展它的内容来填充的宽度。

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.

解决方案

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.

这篇关于编程加入行TableLayout导致不正确的视图层级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆