2017-01-03 175 views
1

我已經在佈局中動態創建了多個按鈕。現在,我想從佈局中移除單擊的按鈕。 例如: - enter image description hereenter image description hereenter image description here如何從佈局中刪除按鈕?

+0

的onclick按鈕使其可見性GONE – Redman

+0

button.setVisibility(View.INVISIBLE);將給按鈕的地方空的空間 –

+1

可能的重複[如何刪除一個按鈕或使其在Android中不可見?](http://stackoverflow.com/questions/4127725/how-can-i-remove-a - 按鈕或 - 使 - 它隱形功能於機器人) –

回答

0

的onClick設置button.setVisibility(View.GONE);

0

如果你想這樣做的每一個按鈕,你可以讓他們所有,然後刪除激活的人的

ArrayList<View> allButtons; 

//Get all buttons from the selected layout, then put them in an arraylist 
allButtons =((LinearLayout)findViewById(R.id.button_container)).getTouchables(); 

//loop on each button and remove the activated ones 
foreach (Button b : allButtons){ 
    if (b.isActivated()){ 
     b.setVisibility(View.GONE); 
    } 
} 
0
LinearLayout parent = new LinearLayout(this); 

     parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     parent.setOrientation(LinearLayout.HORIZONTAL); 
     for (int i = 0 ; i < 10 ; i++) { 
      Button b = new Button(this); 
      b.setOnClickListener(new OnClickListener() { 
       public void onClick(View view) { 
       view.setVisibility(View.GONE); 
       } 
      });  
      b.setText("Primary"); 
      Drawable image = ContextCompat.getDrawable(getApplicationContext(), R.drawable.your_image); 
      image.setBounds(0, 0, 60, 60); 
      b.setCompoundDrawables(null, null, image, null); 
      parent.addView(b); 
     }