我想将相同的 View 添加到一个新的 RelativeLayout 中,该布局之前已添加到另一个 View 中。
RelativeLayout1
-- View1
-- ScrollView
-- Linear Layout2
我有一个添加到线性布局 2 的 View ,它被添加到 ScrollView 。我想将相同的 subview 添加到 RelativeLayout1 以代替 View1。
我以这种方式添加 View ,
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);
现在我想在 View1 的位置将列表中的相同 View 添加到 RelativeLayout1
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
并将克隆添加到第二个父级。
关于android - 将 subview 添加到两个布局(多个父级),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18666914/