gpt4 book ai didi

java - 将 View 添加到 Android LinearLayout

转载 作者:行者123 更新时间:2023-12-01 13:36:53 29 4
gpt4 key购买 nike

我有 Vertical ScrollView,我将在单击按钮后添加一些 View 。这是 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);

}

问题是它只能工作一次。当我第二次单击该按钮时,应用程序崩溃了。预先感谢您。

最佳答案

您的日志显示:

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

您尝试在多次单击时多次添加相同的layoutContainer和textEmpty。这会失败,因为它们已经被添加,因此已经有一个父级。 (您不能将相同的 View 添加到多个父级)

解决方案我猜你想在每次按下按钮时创建一个新的 View 并将其添加到你的layoutScroll中。您应该为每次点击再次调用您的 init 方法:

public void onCLick_Add(View v){
initInterface();
layoutScroll.addView(layoutContainer);
layoutScroll.addView(textEmpty);
}

我还建议将 initInterface() 分为:

private View createLayoutContainer();
private View createTextEmpty();

确保在外部初始化scrollView(例如onCreate),并在createLayoutContainer() 中局部声明layoutContainer 之类的 View ,而不是全局声明。

这里是它的外观 fragment :

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());
}

关于java - 将 View 添加到 Android LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21207930/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com