我有Vertical ScrollView,我会在按钮点击后添加一些视图。这是滚动视图的xml
<ScrollView
android:background = "@drawable/border"
android:layout_weight="31"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<LinearLayout
android:id="@+id/layoutScroll"
android:padding = "10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layoutContainer"
android:padding="5dp"
android:orientation="vertical"
android:background="@drawable/border_2"
android:layout_width="fill_parent"
android:layout_height = "wrap_content">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent">
<TextView
android:id="@+id/textWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/word"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textNumber"
android:background = "@drawable/border_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text=" 1 "
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<EditText
android:id="@+id/editWord"
android:layout_marginTop="3dp"
android:layout_gravity="left"
android:inputType="text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:scrollbars="horizontal"/>
<TextView
android:id="@+id/textDefinition"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "@string/definition" />
<EditText
android:id="@+id/editDefinition"
android:gravity="top|left"
android:inputType="textMultiLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="3"
android:maxLines="3"
android:scrollbars="vertical"/>
</LinearLayout>
<TextView
android:id="@+id/textEmpty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
在这个函数中,我创建了相同的对象,用于将它们添加到LinearLayout
private void initInterface (){
layoutScroll = (LinearLayout) findViewById (R.id.layoutScroll);
layoutContainer = new LinearLayout(getApplicationContext());
relativeLayout = new RelativeLayout(getApplicationContext());
textWord = new TextView(getApplicationContext());
textDefinition= new TextView(getApplicationContext());
textNumber = new TextView(getApplicationContext());
textEmpty = new TextView(getApplicationContext());
editWord = new EditText(getApplicationContext());
editDefinition = new EditText(getApplicationContext());
layoutContainer.setPadding(5, 5, 5, 5);
layoutContainer.setOrientation(LinearLayout.VERTICAL);
layoutContainer.setBackgroundResource(R.drawable.border_2);
LinearLayout.LayoutParams param1 =
new LayoutParams(LayoutParams.MATCH_PARENT , LayoutParams.WRAP_CONTENT );
layoutContainer.setLayoutParams(param1);
RelativeLayout.LayoutParams param2 =
new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(param2);
textWord.setLayoutParams(param2);
textWord.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textWord.setText("Word:");
RelativeLayout.LayoutParams param3=
new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
param3.addRule(RelativeLayout.ALIGN_PARENT_TOP);
param3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textNumber.setBackgroundResource(R.drawable.border_1);
textNumber.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textNumber.setLayoutParams(param3);
relativeLayout.addView(textWord);
relativeLayout.addView(textNumber);
LinearLayout.LayoutParams param4 = param1;
param4.setMargins(0, 3, 0, 0);
editWord.setGravity(Gravity.LEFT);
editWord.setTextAppearance(this,android.R.style.TextAppearance_Medium);
editWord.setLines(1);
editWord.setHorizontallyScrolling(true);
editWord.setLayoutParams(param4);
textDefinition.setLayoutParams(param1);
textDefinition.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textDefinition.setText("Definition:");
editDefinition.setGravity(Gravity.TOP | Gravity.LEFT);
editDefinition.setSingleLine(false);
editDefinition.setLayoutParams(param1);
editDefinition.setMaxLines(3);
editDefinition.setLines(3);
editDefinition.setVerticalScrollBarEnabled(true);
editDefinition.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
textEmpty.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textEmpty.setLines(1);
textEmpty.setLayoutParams(param1);
layoutContainer.addView(relativeLayout);
layoutContainer.addView(editWord);
layoutContainer.addView(textDefinition);
layoutContainer.addView(editDefinition);
}
然后我将它们添加到布局中。
public void onCLick_Add(View v){
layoutScroll.addView(layoutContainer);
layoutScroll.addView(textEmpty);
}
问题在于它只能工作一次。当我第二次点击该按钮时,应用程序崩溃了。 提前谢谢。
答案 0 :(得分:1)
你的日志说:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
多次单击时,您尝试多次添加相同的layoutContainer和textEmpty。这会失败,因为它们已经添加,因此已经有了父级。 (您不能将同一个视图添加到多个父级)
<强>解决方案强> 我想您每次按下按钮并将其添加到layoutScroll时都要创建一个新视图。每次点击都应该再次调用init-method:
public void onCLick_Add(View v){
initInterface();
layoutScroll.addView(layoutContainer);
layoutScroll.addView(textEmpty);
}
我还建议将initInterface()划分为:
private View createLayoutContainer();
private View createTextEmpty();
确保在外部(例如onCreate)初始化scrollView,并在createLayoutContainer()中声明layoutContainer localy等视图而不是全局。
这是一个代码片段:
private View createLayoutContainer(){
LinearLayout layoutContainer = new LinearLayout(getApplicationContext());
RelativeLayout relativeLayout = new RelativeLayout(getApplicationContext());
TextView textWord = new TextView(getApplicationContext());
...
return layoutContainer;
}
public void onCLick_Add(View v){
layoutScroll.addView(createLayoutContainer());
layoutScroll.addView(createTextEmptyView());
}
答案 1 :(得分:0)
问题出现的原因是ScrollView
只能在子视图上显示。换句话说,ScrollView
可以有LinearLayout
或RelativeLayout
或TextView
等。更新您的代码,以便在LinearLayout
内添加新的内部视图( layoutScroll )代替ScrollView
。
答案 2 :(得分:0)
正如我在评论中所指出的,错误是:Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
这些错误的清单。
正如本文the specified child already has a parent
所指出的那样仔细检查所有addView调用。
请确保不要再添加任何视图。当一个View已经准备好了 使用过(例如,你用findViewById得到它,不要在它上面使用addView。
如果要添加视图,请使用带有新视图的addView。你可以加 一个视图中有几个这样的新视图,但是你不能添加那个视图 多次查看。
您只能通过更改某些内容来重复使用视图 东西。您可以重复使用变量,但是如果需要,则需要创建新视图 您想使用addView重新添加它。