简体   繁体   English

Android-从RelativeLayout移除ImageView

[英]Android - Removing an ImageView from a RelativeLayout

How can I remove an ImageView from a RelativeLayout? 如何从RelativeLayout中删除ImageView? For some reason RelativeLayout.removeView(iv); 由于某些原因, RelativeLayout.removeView(iv); doesn't work. 不起作用。 I'm creating the ImageView programmatically like this: 我正在以编程方式创建ImageView,如下所示:

final ImageView iv = new ImageView(MainActivity.this); But this doesn't work. 但这是行不通的。

I don't want to use iv.setVisibility(View.GONE); 我不想使用iv.setVisibility(View.GONE); because this doesn't really remove the image, it just sets the visibility. 因为这并不能真正删除图像,它只是设置可见性。

Why isn't this working? 为什么这不起作用? Is there any alternative? 有没有其他选择?

Just a code snip below may help you understand how dynamic tag works : 下面的代码片段可以帮助您了解动态代码的工作原理:

   addBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.row, null);
                Button buttonRemove = (Button) addView
                        .findViewById(R.id.remove);
                addView.setTag(tag);
                buttonRemove.setTag(tag);
                dynamicLayoutsTags.add(tag);
                Container.addView(addView);
                tag++;
                buttonRemove.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // ((LinearLayout) addView.getParent())
                        // .removeView(addView);
                        Integer removeTag = (Integer) v.getTag();
                        View deleteView = Container.findViewWithTag(removeTag);
                        Container.removeView(deleteView);
                        dynamicLayoutsTags.remove(removeTag);
                    }
                });

            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM