2017-03-25 44 views
-2

所以基本上我的程序應該在每次單擊另一個按鈕時在單獨的活動中創建一個按鈕。但每次添加按鈕時,我添加的上一個按鈕消失(按鈕水平移動)。按鈕不斷消失,因爲我試圖添加更多

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


} 
} 

回答

0

這是因爲您正在將按鈕添加到另一個相同的位置。你的按鈕不會消失,只是你正在渲染前一個按鈕。

你可以通過很多方式正確地做到這一點,比如像保持水平位置一樣並改變垂直位置(確保你在佈局中有滾動視圖),一個接一個地改變新按鈕的位置。

或者,使用RecyclerView,其中孩子只有一個按鈕。每次你需要一個新的按鈕,你只需在RecyclerView的底部添加一個新的孩子。

+0

我可以打印出不同位置的按鈕,並且超時它出現在上一個按鈕消失的不同位置。所以我仍然認爲按鈕正在消失。 –

0

嘗試這會爲你

拷貝在你的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> 

工作,這是MainActivity.java

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

} 

}

+0

您的xml文件似乎不起作用。我收到各種標籤問題。 –

+0

在底部添加

+0

謝謝!你知道一種讓你的代碼在另一列或者行中打印按鈕的方法嗎?它似乎只在一個方向上打印按鈕,我不確定如何調整它,以便一旦按下按鈕它就會移動到另一行。 –

0

我檢查了你的代碼,你只是在代碼中添加一個按鈕。 並且for循環被削減,目前無法運行。 另外,如果您打開for循環,則使用Relative佈局。所以在這種情況下,您必須設置按鈕左側或右側的對象ID。 請嘗試這樣。

  1. 請刪除斜槓並用於循環。 請將RelativeLayout更改爲LinearLayout。
  2. 如果不起作用,請更改現貨默認值。 我從來沒有使用過對象ID爲0,所以不知道它可以工作。
+0

好吧,我改變了所有這一切,但我仍然得到一個錯誤,因爲現在它給了我一個運行時錯誤,並說我試圖調用addView上一個孩子沒有在其父母上調用removeView。我不知道如何去解決這個問題。你有什麼想法? –