【发布时间】:2014-06-05 13:25:43
【问题描述】:
这是我的布局 xml 文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/container1"
android:tag="1"
//... >
<TextView
android:id="@+id/txt1"
android:tag="1"
//... />
</LinearLayout>
<LinearLayout
android:id="@+id/container2"
android:tag="2"
//... >
<TextView
android:id="@+id/txt2"
android:tag="2"
//... />
</LinearLayout>
//...
它有 2 个容器,每个容器中有一个 textview。现在,当我触摸一个按钮时,我需要容器来交换文本视图。为此,这是我的方法:
person1 = (TextView) findViewById(R.id.txt1);
person2 = (TextView) findViewById(R.id.txt2);
place1 = (LinearLayout) findViewById(R.id.place1);
place2 = (LinearLayout) findViewById(R.id.place2);
-
place1.removeView(person1);
place2.removeView(person2);
place1.addView(person2);
place2.addView(person1);
但这就是我在 LogCat 上得到的:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
【问题讨论】:
标签: java android view android-linearlayout