Android 通过单击图像按钮动态生成按钮

Android 通过单击图像按钮动态生成按钮,android,Android,我想通过点击一个图像按钮来动态生成一个按钮,在XML中我提到了图像按钮,我在.java类中也调用了它。我的xml如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/theme" android:layout_width="match_parent" andro

我想通过点击一个图像按钮来动态生成一个按钮,在XML中我提到了图像按钮,我在.java类中也调用了它。我的xml如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/theme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageButton android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/plus1" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/welcomemilan" />

</LinearLayout>

请在这方面提供帮助

您可以使用此代码添加按钮Dynamicly

Button myButton = new Button(this);
myButton.setText("Add Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.theme);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);

将此代码放入onclick函数中并调整它

您可以使用此代码动态添加按钮

Button myButton = new Button(this);
myButton.setText("Add Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.theme);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);

将此代码放入onclick函数中并调整它

您可以使用此代码动态添加按钮

Button myButton = new Button(this);
myButton.setText("Add Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.theme);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);

将此代码放入onclick函数中并调整它

您可以使用此代码动态添加按钮

Button myButton = new Button(this);
myButton.setText("Add Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.theme);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);

将此代码放入onclick函数中,并对其进行调整

您的布局已包含该按钮。这就是为什么你会得到这个例外

如果要向布局动态添加按钮,则必须在每次迭代时创建该按钮的新实例:

for(int j =0; j<10;j++){
  Button newButton = new Button(MainActivity.this);
  l.addView(newButton);
}

for(int j=0;j您的布局已经包含该按钮。这就是为什么您会得到该异常

如果要向布局动态添加按钮,则必须在每次迭代时创建该按钮的新实例:

for(int j =0; j<10;j++){
  Button newButton = new Button(MainActivity.this);
  l.addView(newButton);
}

for(int j=0;j您的布局已经包含该按钮。这就是为什么您会得到该异常

如果要向布局动态添加按钮,则必须在每次迭代时创建该按钮的新实例:

for(int j =0; j<10;j++){
  Button newButton = new Button(MainActivity.this);
  l.addView(newButton);
}

for(int j=0;j您的布局已经包含该按钮。这就是为什么您会得到该异常

如果要向布局动态添加按钮,则必须在每次迭代时创建该按钮的新实例:

for(int j =0; j<10;j++){
  Button newButton = new Button(MainActivity.this);
  l.addView(newButton);
}

for(int j=0;jjava.lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()。

日志cat中的这一行表示一个视图只能有一个父视图。因此,当您尝试在循环中多次添加b1**时,就会产生此问题。若要解决此问题,您需要为每个循环迭代创建一个单独的按钮实例

像这样的事情应该解决:

 for(int j =0; j<10;j++)
    {
    Button b1 = new Button(getApplicationContext());
    b1.setText("Add");
    l.addView(b1);
    Log.i(TAG, "the button is pressed"+j);
    }

for(int j=0;jjava.lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()。

日志cat中的这一行表示一个视图只能有一个父视图。因此,当您尝试在循环中多次添加b1**时,就会产生此问题。若要解决此问题,您需要为每个循环迭代创建一个单独的按钮实例

像这样的事情应该解决:

 for(int j =0; j<10;j++)
    {
    Button b1 = new Button(getApplicationContext());
    b1.setText("Add");
    l.addView(b1);
    Log.i(TAG, "the button is pressed"+j);
    }

for(int j=0;jjava.lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()。

日志cat中的这一行表示一个视图只能有一个父视图。因此,当您尝试在循环中多次添加b1**时,就会产生此问题。若要解决此问题,您需要为每个循环迭代创建一个单独的按钮实例

像这样的事情应该解决:

 for(int j =0; j<10;j++)
    {
    Button b1 = new Button(getApplicationContext());
    b1.setText("Add");
    l.addView(b1);
    Log.i(TAG, "the button is pressed"+j);
    }

for(int j=0;jjava.lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()。

日志cat中的这一行表示一个视图只能有一个父视图。因此,当您尝试在循环中多次添加b1**时,就会产生此问题。若要解决此问题,您需要为每个循环迭代创建一个单独的按钮实例

像这样的事情应该解决:

 for(int j =0; j<10;j++)
    {
    Button b1 = new Button(getApplicationContext());
    b1.setText("Add");
    l.addView(b1);
    Log.i(TAG, "the button is pressed"+j);
    }


for(int j=0;jif我在上述过程中只得到了一个按钮,而不是10@blackbelt。但感谢我至少得到了一个。但是请告诉我如何获得10个按钮。尝试将布局方向从水平链接到垂直,
android:orientation=“vertical”
如何提高@blackbelt的声誉,请告诉我如果我在上述过程中做到了,我只得到了一个按钮,而不是10个@blackbelt。但谢谢,我至少得到了一个。但是请告诉我如何获得10个按钮。尝试将布局方向从水平链接到垂直,
android:orientation=“vertical”
如何提高@blackbelt的声誉,请告诉我如果我在上述过程中做到了,我只得到了一个按钮,而不是10个@blackbelt。但谢谢,我至少得到了一个。但是请告诉我如何获得10个按钮。尝试将布局方向从水平链接到垂直,
android:orientation=“vertical”
如何提高@blackbelt的声誉,请告诉我如果我在上述过程中做到了,我只得到了一个按钮,而不是10个@blackbelt。但谢谢,我至少得到了一个。但是请告诉我如何获得10个按钮。尝试将布局方向从水平链接到垂直,
android:orientation=“vertical”
如何提高@blackbelt的声誉,请告诉我在这个过程中,我只得到一个Add button@Eu.Dr。请告诉我如何得到10个按钮。删除循环,只添加一个按钮==>这会显示吗?此外,你需要设置layoutParams:layoutParams keyPanelParams=新的layoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);b1.setLayoutParams(keyPanelParams);是的,在删除for循环并放置LayoutParams之后,我得到了一个按钮的输出,之后我调用了for循环,它也工作得很好。非常感谢buddy.Pls。别忘了投票选出好的答案并接受正确的答案。@TortoiseWalkerSorry…因为反应太晚。…。@Eu.DrIn在这个过程中,我只得到一个Add按钮@Eu.Dr。请告诉我如何获得10个按钮。删除循环,只添加一个按钮==>是否显示此按钮?此外,您需要设置layoutParams:layoutParams keyPanelParams=新的layoutParams(layoutParams.WRAP_CONTENT,layoutParams.WRAP_CONTENT);b1.setLayoutParams(keyPanelParams);ya在删除for循环并放置LayoutParams之后,我得到了一个bu的输出