简体   繁体   English

如何再次将以编程方式创建的linearlayout添加到另一个linearlayout

[英]How can I add again a programmatically created linearlayout to another linearlayout

I have a child linearlayout created programmatically and added on a parent linearlayour programmatically. 我有一个以编程方式创建的线性子布局,并以编程方式添加到了一个线性父布局。

parentlayout.addView(childlayout)

But, the I want to add again the child layout to another linearlayout, it returns an error, is it possible? 但是,我想再次将子布局添加到另一个linearlayout中,它返回错误,可以吗? What is the best way to do that? 最好的方法是什么?

Create another instance of the same class , and set the same properties . 创建相同class另一个instance ,并设置相同的properties I mean Inflate your View again if needed. 我的意思是,如有需要,请再次为您的View Because A single View cannot be a child of two ViewGroup parents. 因为单个View不能是两个ViewGroup父级的子级。

Each view can have only one parent possible. 每个视图只能有一个父视图。 So you can not add same child to another linear layout. 因此,您不能将同一子项添加到另一个线性布局中。 What you can do is make another instance, apply same properties which you given to previous child. 您可以做的是创建另一个实例,将您赋予先前子级的相同属性应用于该子级。 and then add this instance to the parent view. 然后将此实例添加到父视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM