我想将相同的视图添加到新的RelativeLayout中,该新的RelativeLayout之前已添加到另一个视图中。
RelativeLayout1
-- View1
-- ScrollView
-- Linear Layout2
我有一个添加到线性layout2的视图,该视图已添加到scrollview。我想在View1的位置向RelativeLayout1添加相同的子视图。
我以这种方式添加视图,
MyCustomScrollView scrollView = new MyCustomScrollView(context);
layout2 = new LinearLayout(context);
for(int i=0;i<10;i++)
layout2.addView(list.get(i));
scrollView,add(layout2);
relativeLayout1.addView(scrollView);
现在,我想将相同的视图从列表添加到RelativeLayout1中,以代替View1
RelativeLayout1.remove(view1);
RelativeLayout1.addView(list.get(0),0);
Causes
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:2062)
at android.view.ViewGroup.addView(ViewGroup.java:1957)
at android.view.ViewGroup.addView(ViewGroup.java:1914)
一个View
不能是两个ViewGroup
父母的孩子。
随时克隆View
并将克隆添加到第二个父对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句