如何清除 android 中的子布局?

How to clear child layout in android?

我正在开发一个 android 应用程序,我在其中创建相对布局上的动态图像箭头。图像是在相对布局的点击区域的 x,y 坐标上创建的。下面是我正在使用的代码。

presciptionScreenArrowImg.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (canSelectMedianStatus == 2) {
                if (event == simulationEvent)
                    return false;
                int action = event.getAction();
                int x = (int) event.getX();
                int y = (int) event.getY();
                Log.e("onTouchListener", "User touch at X:" + x + " Y:" + y);
                pointerArrow = new ImageView(getApplication());
                pointerArrow.setImageResource(R.drawable.pointer);
                pointerArrow.setId(imageArrayTag);
                imageArrayTag++;
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
                params.topMargin = y;
                params.leftMargin = x;
                pointerArrow.setLayoutParams(params);
                presciptionScreenArrowImg.addView(pointerArrow);
                long length = 0;
                if (action == MotionEvent.ACTION_DOWN) {
                 //                    click(v, x, y);
                }
            }
            return false;
        }
    });

现在,我需要点击按钮时最后绘制的图像应该首先删除。基本上我需要一个撤消功能来删除图像作为后进先出结构。

将视图存储在队列中,添加新视图时检查队列是否已满。如果是,从队列中弹出一个视图,然后调用 presciptionScreenArrowImg.remove(poppedView);

让我们考虑一下 presciptionScreenArrowImg 是您的主要布局,其中包含您的所有视图以便删除

int index=presciptionScreenArrowImg.getChildCount();
if(index>0)
presciptionScreenArrowImg.removeViewAt(index-1);

从上面获取 child 的计数并删除最后一个视图,如果您有任何问题,请告诉我