以编程方式从 LinearLayout 中删除 TextView 时出现问题 [英] Having a problem removing TextViews from a LinearLayout programmatically

查看:11
本文介绍了以编程方式从 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 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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