[英]Having a problem removing TextViews from a LinearLayout programmatically
I am programmatically adding TextViews to a LinearLayout, and deleting them on touch. 我以编程方式将TextViews添加到LinearLayout,并在触摸时删除它们。 It all works fine except when the last TextView is touched it doesn't get removed. 一切正常,除非触摸最后一个TextView,它不会被删除。 If I do anything else on the screen like get rid of the keyboard or scroll down at all, the last TextView will be deleted, which makes me think it's a refresh problem, but I have no idea how to solve that. 如果我在屏幕上做任何其他事情,比如摆脱键盘或向下滚动,最后的TextView将被删除,这让我觉得这是一个刷新问题,但我不知道如何解决这个问题。
Here's some of the code I'm using: 这是我正在使用的一些代码:
final TextView tv1 = new TextView(this);
tv1.setText("Test");
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearlayout1.removeView(tv1);
}
});
I have also added this code in to try to solve the problem but it didn't change anything: 我还添加了此代码以尝试解决问题,但它没有改变任何东西:
if (linearlayout1.getChildCount() == 1) {
linearlayout1.removeAllViewsInLayout();
}
This sounds more of a bug in Android, but one thing you could try is hiding your TextView
before removal: 这听起来更像Android中的一个错误,但您可以尝试的一件事是在删除之前隐藏TextView
:
tv1.setVisibility(View.GONE)
Or alternatively you could add: 或者您可以添加:
linearlayout1.invalidate()
after removal of the last item to trigger redrawing. 删除最后一项后触发重绘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.