问题
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