问题描述
如何从 Android 应用程序中添加和删除诸如 TextView
之类的视图,就像在原始库存 Android 联系人屏幕上一样,您可以在其中按下字段右侧的小图标,然后它会添加或删除一个由 TextView
和一个 editTextView
组成的字段(据我所知).
How do I add and remove views such as TextView
s 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?
推荐答案
ViewParent
s 一般不能删除视图,但 ViewGroup
s 可以.你需要将你的父级转换为 ViewGroup
(如果它是一个 ViewGroup
)来完成你想要的.
ViewParent
s in general can't remove views, but ViewGroup
s 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 中的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!