动态添加和删除 Android 中的视图? [英] Add and Remove Views in Android Dynamically?

查看:38
本文介绍了动态添加和删除 Android 中的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Android 应用程序中添加和删除诸如 TextView 之类的视图,就像在原始库存 Android 联系人屏幕上一样,您可以在其中按下字段右侧的小图标,然后它会添加或删除一个由 TextView 和一个 editTextView 组成的字段(据我所知).

How do I add and remove views such as TextViews from Android app like on the original stock Android contacts screen where you press a small icon on the right side of a field and it adds or deletes a field which consists of a TextView and an editTextView (from what I can see).

有关如何实现这一目标的任何示例?

Any examples on how to achieve this?

推荐答案

ViewParents 一般不能删除视图,但 ViewGroups 可以.你需要将你的父级转换为 ViewGroup(如果它是一个 ViewGroup)来完成你想要的.

ViewParents in general can't remove views, but ViewGroups can. You need to cast your parent to a ViewGroup (if it is a ViewGroup) to accomplish what you want.

例如:

View namebar = View.findViewById(R.id.namebar);
((ViewGroup) namebar.getParent()).removeView(namebar);

注意所有Layout都是ViewGroup.

这篇关于动态添加和删除 Android 中的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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