Android uses WindowManager to implement hover windows

Source: Internet
Author: User

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"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.