动态添加和删除表格行 - Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1407100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically adding and removing table rows - Android
提问by brodie31k
I am trying to dynamically add and remove rows from a TableLayout.
我正在尝试从 TableLayout 中动态添加和删除行。
The layout is defined in an xml file.
布局在 xml 文件中定义。
I am able to successfully remove a row, but when I call the corresponding addView command nothing happens.
我能够成功删除一行,但是当我调用相应的 addView 命令时没有任何反应。
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)
但我不知道如何确定行的索引,似乎没有一种方法可以做到这一点。有谁知道怎么做?(即。如果我不知道索引是 4,我怎么能弄明白)
Edit: included XML. this is just the row in question, there are other rows above and below it
编辑:包含 XML。这只是有问题的行,它的上方和下方还有其他行
<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>
回答by CommonsWare
Use hierarchyviewer
(in your SDK tools/
directory) to determine if the row is truly not being added, or is being added but some layout parameters are messed up and so it is not appearing on-screen.
使用hierarchyviewer
(在您的 SDKtools/
目录中)确定该行是否真的没有被添加,或者正在被添加但一些布局参数被搞砸了,所以它没有出现在屏幕上。
回答by einschnaehkeee
public int indexOfChild (View child)
public View getChildAt (int index)
Both methods provided by TableLayout. ;-)
TableLayout 提供了这两种方法。;-)