2017-01-03 135 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); 
     }