I'm trying to make my own basic 'help overlay'. I have achieved this much so far Take a look

this was done by calling the addContentView with an instance of a custom view passed in. However i will only be able to hide the custom view instead of completely removing it after I am done with it.

It is very basic so far so there is no flair or pizazz yet. I am trying to further improve its ease of use. https://stackoverflow.com/a/10217050/3194316 this stack overflow answer suggests setting the content view to a dynamically created Framelayout and inflating the views. I have added the code

FrameLayout layout = new FrameLayout((Context) activity);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);

View parentView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
((ViewGroup) parentView.getParent()).removeAllViews();
activity.setContentView(layout);

layout.addView(parentView);
layout.addView(this);

where this is an instance of the custom view that sets the overlay shown below. activity is an instance of the activity in which this is presented in. I was at first getting an error that the parentView already had a parent, so this is why View parentView = activity.getWindow().getDecorView().findViewById(android.R.id.content); ((ViewGroup) parentView.getParent()).removeAllViews(); was added. I am now unforunately receiving a stack overflow error, and I cannot seem to understand why. Is there a better approach to this situation?

SOLUTION

Wrapping everything in a popup window allows the entire overlay to be dismissed properly by the android System.

FrameLayout layout = new FrameLayout(context.get());
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);

LayoutInflater inflater = (LayoutInflater) context.get().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);

View overlayBase = inflater.inflate(R.layout.help_frame, null, false);
layout.addView(this);
layout.addView(overlayBase);

popupWindow = new PopupWindow(context.get());
popupWindow.setContentView(layout);
popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.showAtLocation(this, Gravity.CENTER, 0, 0);

context was a class level WeakReference variable for the custom view, instantiated when the constructor was called

In case anyone is interested, here is the code I used to create the popupwindow

Here is the (not final) result Take a look

有帮助吗?

解决方案

If you want a dynamically appearing View anchored to another View you should use a PopupWindow

Is there a simple example of the PopupWindow class using Android v2.0?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top