从自定义布局

时间:2017-07-02 20:15:40

标签: android android-imageview

我使用FlowLayout。我这里有button这样的外面。

enter image description here

按下按钮中的按钮会在按下按钮时添加相同的图片。我想出了如何添加图片,但如何删除图片?

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

            }
        }
    });

2 个答案:

答案 0 :(得分:0)

要删除添加到FlowLayout的最后一个ImageView,您只需执行以下操作:

flowLayout.removeViewAt(flowLayout.getChildCount() - 1);

答案 1 :(得分:0)

如果你必须删除多个,你可以试试这个:

  , , feature1

       [,1] [,2] [,3] [,4]
 [1,]  0.5 0.52  0.5  0.3
 [2,]  0.5 0.50  0.5  0.5

 , , feature2

      [,1] [,2] [,3] [,4]
 [1,]  0.7 0.72  0.7  0.7
 [2,]  0.3 0.30  0.3  0.3