简体   繁体   中英

Deleting Dynamic Relative layout by the click of button inside the respective layout

I have a dynamic RelativeLayout with a Imageview inside it. Once the user clicks on that ImageView , I would like to change the delete the parent view ( 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?

您可以通过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

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


        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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