以编程方式从 LinearLayout 中删除 TextView 时出现问题

编程入门 行业动态 更新时间:2024-10-18 01:30:46
本文介绍了以编程方式从 LinearLayout 中删除 TextView 时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我以编程方式将 TextViews 添加到 LinearLayout,并在触摸时删除它们.一切正常,除非最后一个 TextView 被触摸时它没有被删除.如果我在屏幕上做任何其他事情,比如摆脱键盘或向下滚动,最后一个 TextView 将被删除,这让我认为这是一个刷新问题,但我不知道如何解决.

I am programmatically adding TextViews to a LinearLayout, and deleting them on touch. It all works fine except when the last TextView is touched it doesn't get removed. 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.

这是我正在使用的一些代码:

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

推荐答案

这听起来更像是 Android 中的一个错误,但您可以尝试的一件事是在删除之前隐藏您的 TextView:

This sounds more of a bug in Android, but one thing you could try is hiding your TextView before removal:

tv1.setVisibility(View.GONE)

或者您也可以添加:

linearlayout1.invalidate()

在删除最后一项后触发重绘.

after removal of the last item to trigger redrawing.

更多推荐

以编程方式从 LinearLayout 中删除 TextView 时出现问题

本文发布于:2023-11-23 20:36:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622791.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   LinearLayout   TextView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!