如何从布局中删除按钮?

How to remove Button from layout?

我在布局中创建了多个按钮 dynamically.Now,我想从布局中删除点击的按钮。 例如:-

点击设置button.setVisibility(View.GONE);

如果你想在每个按钮上都这样做,你可以得到它们,然后删除激活的按钮

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