Original address: http://www.3g-edu.org/news/art027.htm
Here's a look at how to achieve this with WindowManager.
by WindowManager's AddView () method, and set the relevant properties of the Windowmanager.layoutparams, you can add the required view to the WindowManager, and depending on the Windowmanager.layoutparams attribute, you can achieve different effects. For example, create a system top-level window to achieve the hover window effect. If you need to remove the view from WindowManager, you only need to call Removeview ().
Here's a simple example to explain how to implement a hover window effect.
First, get the Windoemanager object:
WindowManager Wmanager = Getapplicationcontext (). Getsystemservice (Context. Window_ SERVICE);
Next, get the Windowmanager.layoutparams object, prepare for the subsequent setting of the relevant parameters:
Private Windowmanager.layoutparams wmparams=new windowmanager.layoutparams ();
Then, to set the relevant window layout parameters, to achieve the suspension window effect, the main parameters to be set are:
Wmparams.type = Layoutparams.type_phone; Set Window type
Wmparams.format = pixelformat.rgba_8888; Format picture, effect is transparent background
/*
* The effect of the Flags property below is the same as "lock". The suspended window is not touch, accepts no events, and does not affect the response of subsequent events.
*/
Wmparams.flags=layoutparams.flag_not_touch_modal |
layoutparams.flag_not_focusable | layoutparams.flag_not_touchable;
Wmparams.gravity = gravity.right| Gravity. center_vertical; Adjust the hover window to the right middle
Set X, y initial values as the origin in the upper-left corner of the screen
wmparams.x = 0;
Wmparams.y = 0;
Setting the hover window length-width data
Wmparams.width = WindowManager.LayoutParams.WRAP_CONTENT;;
Wmparams.height =windowmanager.layoutparams.wrap_content;
You can then add the view to the window that you want to add to the hover window:
if (view.getparent==null)//If view is not added to a parent component, add WindowManager
Wmanager.addview (View,wmparams);
Where views are the view components that need to be placed in a suspended window.
If you want to remove it from WindowManager, you can execute the following statement:
if (View.getparent ()!=null)
Wmanager.removeview (view);
Finally, it is important to note that if you want to use a hover window, you need to include the following permissions in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>