Java 当我尝试添加更多时,按钮不断消失

Java 当我尝试添加更多时,按钮不断消失,java,android,Java,Android,因此,基本上我的程序应该在每次单击另一个按钮时在单独的活动中创建一个按钮。但是,每次我添加一个按钮时,我添加的上一个按钮就会消失,按钮会水平移动 import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.content.Intent; import android.

因此,基本上我的程序应该在每次单击另一个按钮时在单独的活动中创建一个按钮。但是,每次我添加一个按钮时,我添加的上一个按钮就会消失,按钮会水平移动

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.LinearLayout;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import java.util.LinkedList;
import android.widget.GridLayout.LayoutParams;

import java.util.Arrays;


public class TestActivity extends AppCompatActivity {
public static int count = 0;
public static int x = 100;
//public static int[] xPos = new int[5];
public static int y = 100;
private boolean stopper = true;
public static int spot = 0;
//public static Button[] buttonArr = new Button[5];
public static LinkedList<Button> buttonList = new LinkedList<Button>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    int b = getIntent().getIntExtra("bob", 0);

    //System.out.println(b);
    if(b==-11978182){
        //System.out.println(buttonList.size());
        //for(int i = 0; i<buttonList.size(); i++) {
            //RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
            //Button addButton = new Button(this);
            //if(stopper) {
                //buttonList.add(addButton);
                //stopper = false;
            //}
            //System.out.println(buttonList.size());
            RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
            Button addButton = new Button(this);
            addButton.setId(spot);
            buttonList.add(addButton);
            System.out.println(buttonList.size());
            buttonList.get(spot).setText("add");
            RelativeLayout layout = new RelativeLayout(getApplicationContext());
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(200, 200);
            layoutParams.setMargins(100*(spot+1), 100, 100, 100); // left, top, right, bottom
            buttonList.get(spot).setLayoutParams(layoutParams);
            //RelativeLayout layout = (RelativeLayout) findViewById(R.id.main);

            //for(int i = 0; i<buttonList.size(); i++) {
                mainLayout.addView(buttonList.get(spot));
                mainLayout.addView(layout);
            //}
            spot = spot + 1;
        //}
    }

    Button galleryButton = (Button) findViewById(R.id.gallery);
    galleryButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(TestActivity.this,Test2.class);
            startActivity(intent);
        }
    });


}
}

这是因为您将按钮一个接一个地添加到同一位置。你的按钮并没有消失,只是在上一个按钮上渲染新按钮

您可以通过多种方式正确执行此操作,如依次更改新按钮的位置,如保持水平位置不变,以及更改垂直位置,确保布局中有滚动视图


或者,使用一个RecyclerView,其中孩子只有一个按钮。每次您需要一个新按钮时,您只需在RecyclerView的底部添加一个新的子项。

试试看,这对您很有用

将其复制到xml布局文件中

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll_main">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/click"
    android:text="click"/>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


<LinearLayout
    android:id="@+id/ll_add_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

</LinearLayout>

}我检查了您的代码,您只在代码中添加了一个按钮。 并且for循环已被删除,此时无法运行。 此外,如果打开for循环,则使用的是相对布局。所以在这种情况下,您必须设置按钮left或right object id。 请像这样试试

请删除斜杠并用于循环。 请将RelativeLayout更改为LinearLayout。 如果不起作用,请更改spot默认值。 我从未将对象id用作0,因此不确定它是否可以工作。
您的xml文件似乎无法工作。我收到各种标签问题。请在底部添加此项谢谢!您知道如何让代码打印其他列或行中的按钮吗?它似乎只在一个方向上打印按钮,我不知道如何调整它,以便在按下按钮后它将移动到另一行。如果ma回答正确,您可以在ma回答之前放弃投票。我可以在不同的位置打印按钮,并且随着时间的推移,它会在不同的位置显示,与前一个按钮不同消失了。因此,我仍然认为按钮正在消失。好吧,我改变了所有这些,但我仍然得到了一个错误,因为现在它给了我一个运行时错误,并说我试图在一个子系统上调用addView,而没有在其父系统上调用removeView。我不知道如何着手解决这个问题。你有什么想法吗?
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_add_btn);
    final Button button = (Button) findViewById(R.id.click);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            LinearLayoutCompat.LayoutParams lparams = new LinearLayoutCompat.LayoutParams(
                    LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
            Button button1 = new Button(getApplicationContext());
            button1.setLayoutParams(lparams);
            button1.setText("added");
            linearLayout.addView(button1);
        }
    });

}