How to remove a layout that was added using addContentView()?

邮差的信 提交于 2019-12-04 19:23:08

问题


I am adding a layout using addContentView(). How can i remove this layout on a Button click ?


回答1:


Assuming contentView is the view that was added via window.addContentView()

((ViewGroup) contentView.getParent()).removeView(contentView);



回答2:


try it

View youAddedView;
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
for (int i = 0; i < rootView.getChildCount(); i++) {
    if(rootView.getChildAt(i) == yourAddedView) {
        // do anything here
    }
}



回答3:


If you already have the reference to the view you can simply just do :

ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
rootView.removeView(viewToRemove);

Instead of looping through the ViewGroup.




回答4:


Unfortunately there's no way of removing a content view that was added with addContentView(). The best you can you do is to call setVisibility(View.GONE) on it, to hide it.

That is why the activity's onContentChanged() only gets called when the content view is set or added to an activity.




回答5:


you can do two things over here you can set the visibility to gone on the click event of the button. OR you can set the layout parameter to layout width and height to 0dp It will hide your layout display



来源:https://stackoverflow.com/questions/9883089/how-to-remove-a-layout-that-was-added-using-addcontentview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!