Android 从活动中删除视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3080577/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:37:02  来源:igfitidea点击:

Removing a View from an Activity

android

提问by Emanuil Rusev

You've got a View defined in a layout file. How do you remove it from you Activity code?

您已经在布局文件中定义了一个视图。你如何从你的活动代码中删除它?

回答by Matt

ViewGroup vg = (ViewGroup)(myView.getParent());
vg.removeView(myView);

should do what you want as far as correctly removing the View from the Activity. The other guys' answer will just make the View invisible, using up resources.

应该做你想做的,只要正确地从活动中删除视图。其他人的回答只会使视图不可见,从而耗尽资源。

回答by isluji

As Androidmentioned in a comment:

正如Android在评论中提到的:

view.setVisibility(View.GONE);

Quoting the Android reference:

引用 Android 参考:

This view is invisible, and it doesn't take any space for layout purposes.

这个视图是不可见的,它不占用任何布局空间。

I think it's the best solution unless you reallyneed to delete the object.

我认为这是最好的解决方案,除非您确实需要删除该对象。