WindowManager for Android Development

Source: Internet
Author: User

WindowManager is an important Service in Android and is globally unique. WindowManager inherits from ViewManager.

WindowManager is mainly used to manage the status, attributes, view addition, deletion, update, window order, and message Collection and Processing of windows. In Android, windows and views are displayed to users. activity is mainly used to handle logic problems, such as lifecycle management and window creation.

There is also an important static class LayoutParams in WindowManager. It can be used to set and obtain some properties of the current window.

 

 

1. Implement floating window:

1. Obtain the WindowManager service:

WindowManager wmManager = (WindowManager) getSystemService (Context. WINDOW_SERVICE );

2. Set WindowManager. LayoutParams Parameters

WindowManager. LayoutParams wmParams = new WindowManager. LayoutParams ();

WmParams. type = LayoutParams. TYPE_PHONE; // set the window type.
WmParams. format = PixelFormat. RGBA_8888; // set the image format. The effect is transparent to the background.

WmParams. flags = LayoutParams. FLAG_NOT_TOUCH_MODAL | LayoutParams. FLAG_NOT_FOCUSABLE | LayoutParams. FLAG_NOT_TOUCHABLE;

WmParams. gravity = Gravity. RIGHT | Gravity. CENTER_VERTICAL; // adjust the floating window to the middle of the RIGHT
WmParams. x = 0; // set the initial values of x and y in the upper-left corner of the screen.
WmParams. y = 0;

WmParams. width = WindowManager. LayoutParams. WRAP_CONTENT; // you can specify the length and width of the floating window.
WmParams. height = WindowManager. LayoutParams. WRAP_CONTENT;

3. Add view to screen

WmManager. addView (view, wmParams );

4. delete a view from the screen

WmManager. removeView (view );

5. You need to add permissions to the floating window.

<Uses-permission android: name = "android. permission. SYSTEM_ALERT_WINDOW"/>

 

2. Obtain the screen size through Display in WindowManager:

WmManager. getdefadisplay display (). getWidth ();

WmManager. getdefadisplay display (). getHeight ();

 

3. Change the Dialog background transparency:

Dialog dg = new Dialog (this );

Window window = dg. getWindow ();

WindowManager. LayoutParams lp = window. getAttributes ();

Lp. alpha = 0.5f;

Window. setAttributes (lp );

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.