以编程方式从自定义布局中删除 ImageView
Remove ImageView programmatically from custom Layout
我在外面用FlowLayout. I have here such a button。
按下布局中的按钮会添加与按下按钮次数相同的图片。我想出了如何添加图片,但如何删除图片?
numberButton.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
@Override
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
// Добавляем новый ImageView
if (oldValue < newValue) {
ImageView imageView = new ImageView(CreateNewTripActivity.this);
imageView.setImageResource(R.drawable.i_travel_logo_1);
ViewGroup.LayoutParams imageViewLayoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageViewLayoutParams.height = 300;
imageViewLayoutParams.width = 300;
imageView.setLayoutParams(imageViewLayoutParams);
imageView.setId();
flowLayout.addView(imageView);
} else {
//Удаляем
AlertDialog.Builder ad = new AlertDialog.Builder(CreateNewTripActivity.this);
ad.setTitle(getResources().getString(R.string.title_delete_person)); // заголовок
ad.setMessage(getResources().getString(R.string.dialog_aushure)); // сообщение
ad.setPositiveButton(getResources().getString(R.string.button_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
Toast.makeText(CreateNewTripActivity.this, "Вы сделали правильный выбор",
Toast.LENGTH_LONG).show();
//TO DO.......
flowLayout.removeView(imageView);
}
});
ad.setNegativeButton(getResources().getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
Toast.makeText(CreateNewTripActivity.this, "Возможно вы правы", Toast.LENGTH_LONG)
.show();
}
});
ad.setCancelable(false);
}
}
});
要删除添加到 FlowLayout 的最后一个 ImageView,您只需执行以下操作:
flowLayout.removeViewAt(flowLayout.getChildCount() - 1);
如果您必须删除多个,您可以试试这个:
int count = constrainLayout.getChildCount();
flowLayout.removeViews(0, count);
我在外面用FlowLayout. I have here such a button。
按下布局中的按钮会添加与按下按钮次数相同的图片。我想出了如何添加图片,但如何删除图片?
numberButton.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
@Override
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
// Добавляем новый ImageView
if (oldValue < newValue) {
ImageView imageView = new ImageView(CreateNewTripActivity.this);
imageView.setImageResource(R.drawable.i_travel_logo_1);
ViewGroup.LayoutParams imageViewLayoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageViewLayoutParams.height = 300;
imageViewLayoutParams.width = 300;
imageView.setLayoutParams(imageViewLayoutParams);
imageView.setId();
flowLayout.addView(imageView);
} else {
//Удаляем
AlertDialog.Builder ad = new AlertDialog.Builder(CreateNewTripActivity.this);
ad.setTitle(getResources().getString(R.string.title_delete_person)); // заголовок
ad.setMessage(getResources().getString(R.string.dialog_aushure)); // сообщение
ad.setPositiveButton(getResources().getString(R.string.button_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
Toast.makeText(CreateNewTripActivity.this, "Вы сделали правильный выбор",
Toast.LENGTH_LONG).show();
//TO DO.......
flowLayout.removeView(imageView);
}
});
ad.setNegativeButton(getResources().getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
Toast.makeText(CreateNewTripActivity.this, "Возможно вы правы", Toast.LENGTH_LONG)
.show();
}
});
ad.setCancelable(false);
}
}
});
要删除添加到 FlowLayout 的最后一个 ImageView,您只需执行以下操作:
flowLayout.removeViewAt(flowLayout.getChildCount() - 1);
如果您必须删除多个,您可以试试这个:
int count = constrainLayout.getChildCount();
flowLayout.removeViews(0, count);