简体   繁体   English

通过单击相应布局内的按钮删除动态相对布局

[英]Deleting Dynamic Relative layout by the click of button inside the respective layout

I have a dynamic RelativeLayout with a Imageview inside it. 我有一个动态的RelativeLayout ,里面有一个Imageview Once the user clicks on that ImageView , I would like to change the delete the parent view ( RelativeLayout ). 一旦用户单击该ImageView ,我想更改删除父视图( RelativeLayout )。 I know I can do this by storing the parent view in a variable, but I'd like to avoid that (I have very good reasons for not wanting this). 我知道我可以通过将父视图存储在一个变量中来做到这一点,但是我想避免这种情况(我有很好的理由不希望这样做)。 Isn't there a way to just access the parent view from the ImageView itself? 是否没有办法仅从ImageView本身访问父视图?

您可以通过getParent方法获取父级View,诸如此类

RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent();

这是您需要的吗?

View view = findViewById(R.id.view);
view.removeView(yourView);

Thanks both for helping ,here's mine for dynamically created layout. 感谢双方的帮助,这里是我动态创建布局的地方。 For Imageview or Button which is also created dynamically, override the onClick function and pass the id or tag to the passing function 对于同样动态创建的Imageview或Button,请覆盖onClick函数并将id或标签传递给传递函数

 private View.OnClickListener HandleOnClick(final ImageView imageView, final int id) {
    return new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            RelativeLayout yourRelLay= (RelativeLayout) view.getParent();
           // yourRelLay.removeView((View) view.getParent());
            yourRelLay.removeAllViews();
            rll.removeView(yourRelLay);


        }
    };

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

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