所以基本上我的程序應該在每次單擊另一個按鈕時在單獨的活動中創建一個按鈕。但每次添加按鈕時,我添加的上一個按鈕消失(按鈕水平移動)。按鈕不斷消失,因爲我試圖添加更多
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);
}
});
}
}
我可以打印出不同位置的按鈕,並且超時它出現在上一個按鈕消失的不同位置。所以我仍然認爲按鈕正在消失。 –