我试图动态地从一个TableLayout中添加和删除行。
的布局在xml文件中定义
我能够成功地删除行,但是当我调用相应的addView命令没有反应。
表=(TableLayout)findViewById(R.id.table);
行=(的TableRow)findViewById(R.id.row);
table.removeView(行);
table.addView(行);
这导致了行被删除,但不被再次添加。
编辑:原来它是如果它被删除从同一位置添加的毕竟只是在屏幕的底部,而不是
我能够通过指定索引将其添加到正确的位置:
table.addView(行,4); // 4发生了行
但我无法弄清楚如何确定行的指数,似乎没有成为一个方法来做到这一点。任何人都知道怎么做呢? (也就是说,如果我不知道该指数4我怎么能明白这一点)
编辑:包括XML。这仅仅是所讨论的行中,有上面和下面的其它行
<的TableRow机器人:ID =@ + ID /行>
< TextView的机器人:ID =@ + ID /字段1
机器人:文本=测试
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:填充=3dip
机器人:TEXTSTYLE =黑体
机器人:TEXTSIZE =18dip
/>
< TextView的机器人:ID =@ + ID /域2
机器人:填充=3dip
机器人:文本=测试
机器人:TEXTSIZE =18dip
机器人:重力=右
/>
< /的TableRow>
解决方案
公众诠释indexOfChild(查看子)
公共查看getChildAt(INT指数)
由TableLayout提供的两种方法。 ; - )
I am trying to dynamically add and remove rows from a TableLayout.
The layout is defined in an xml file.
I am able to successfully remove a row, but when I call the corresponding addView command nothing happens.
table = (TableLayout)findViewById(R.id.table);
row = (TableRow)findViewById(R.id.row);
table.removeView(row);
table.addView(row);
This results in a row being removed, but not being added again.
Edit: It turns out it was adding if after all, just at the bottom of the screen instead of in the same location as it was removed from.
I am able to add it in the correct position by specifying the index:
table.addView(row,4); // 4 happens to the the row
but I can not figure out how to determine the index of the row , there does not seem to be a method to accomplish this. anyone know how do to that? (ie. if I did not know the index was 4 how could I figure that out)
Edit: included XML. this is just the row in question, there are other rows above and below it
<TableRow android:id="@+id/row">
<TextView android:id="@+id/field1"
android:text="testing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textStyle="bold"
android:textSize="18dip"
/>
<TextView android:id="@+id/field2"
android:padding="3dip"
android:text="test"
android:textSize="18dip"
android:gravity="right"
/>
</TableRow>
解决方案
public int indexOfChild (View child)
public View getChildAt (int index)
Both methods provided by TableLayout. ;-)