About Andrews in two ways WindowManager.LayoutParams () of

First, what is WindowManager

       WindowManager Android is an important Service, and is globally unique. WindowManager inherited from ViewManager. Some mainly used to manage the status window, attribute, view add, delete, update, window sequence, message collection and processing. By Context.getSystemService (Context.WINDOW_SERVICE) can be obtained in a manner WindowManager example. Android really is presented to the user's window and view, activity role is mainly to deal with some logical problems, such as life-cycle management and the establishment of the window.

Second, the role of WindowManager ( Reference )

(1), to achieve the floating window:

1. Obtain WindowManager services:

Windowmanager wmManager = (windowmanager) ge systematize Foodservice (Co inner xt.WI CONTRACT CALENDAR_SERVICE); 

2. Set parameters WindowManager.LayoutParams

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

wmParams.type = LayoutParams.TYPE_PHONE; // set type window
      wmParams.format = PixelFormat.RGBA_8888; // set the image format, background transparent effect

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

wmParams.gravity = Gravity.RIGHT | Gravity CENTER_VERTICAL;. // adjust the suspension to the window right middle
      wmParams.x = 0; // upper left corner of the screen as an origin, provided x, y initial value
      wmParams.y = 0;

wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT; // set the floating window length and width data
      wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

3. Add the view to the screen

wmManager.addView(view,wmParams);

4. Delete the view from the screen

wmManager.removeView(view);

The floating window for an add permissions

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

 

(2), acquired by the screen size of WindowManager Display:

wmManager.getDefaultDisplay().getWidth();

wmManager.getDefaultDisplay () getHeight ().

 

(3), change the background transparency Dialog:

Dialog dg = new Dialog(this); 

Window window = dg.getWindow(); 

WindowManager.LayoutParams lp = window.getAttributes(); 

lp.alpha = 0.5f; 

window.setAttributes(lp);


Two, WindowManager use

WindowManager method is very simple, basic three addView used, removeView, updateViewLayout  

In WindowManager there is an important static class LayoutParams. You can set some properties and get it through the current window. View created by addView method WindowManager so created out of View WindowManager.LayoutParams according to different attributes, the effect will be different. For example, to create a system top-level window, floating window to achieve results ( top-level window control )!


For example in suspended window

addView method for adding a suspension window, updateViewLayout method for updating a suspension parameter window, removeView for removing suspended window. Which suspended the parameters window of the need to elaborate.

This class is used to provide WindowManager.LayoutParams suspension parameters required for the window, there are several variables which are often used:

type is used to determine the suspension type window, usually set to 2002, it represents over all applications, but under the status bar.

flags value is used to determine the suspension behavior of the window, for example, can not focus, non-modal dialog boxes, and so on, the property is very large, you can view the document.

gravity is used to determine suspension alignment window, generally aligned to the upper left corner, so that when the drag suspension window of time to facilitate the calculation coordinates.

x value for determining the suspended position of the window, if you want to move laterally suspension window, it is necessary to change this value.

y is used to determine the suspension position of the window, if you want to move longitudinally suspension window, it is necessary to change this value.

value specifies the width suspension width of the window.

value specifies the height suspension height of the window.


Bb = new new the Button the Button (the this);
  bb.setText ( "nimei");
  
   // get WindowManager service:. "Window"
  . WindowManager wManager = (WindowManager) getApplicationContext () getSystemService (Context.WINDOW_SERVICE);
  
      / **
         * Set WindowManager.LayoutParams relevant attributes
         * refer to the particular use SDK documentation
         * /
  WindowManager.LayoutParams wParams new new WindowManager.LayoutParams = ();

  wParams.type = LayoutParams.TYPE_PHONE; // set the window type, type is the key, where "2002" represents system-level window, you can also try the 2003
  wParams.format = PixelFormat.RGBA_8888; //// Format Picture, a transparent background effect
  

  wParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE;
  wParams.gravity = Gravity.RIGHT | Gravity CENTER_VERTICAL;. // adjust the suspension to the window right middle
  wParams.width = WindowManager.LayoutParams. WRAP_CONTENT; // set the floating window length and width data
  wParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
  wManager.addView (BB, wParams); // add BB to view screen
  
  //wManager.removeView(bb); // from the screen remove BB (View)
  // need to add a suspension permissions window <uses-permission android: name = "android.permission.SYSTEM_ALERT_WINDOW" />


 

发布了8 篇原创文章 · 获赞 4 · 访问量 1万+

Guess you like

Origin blog.csdn.net/honey_angle_first/article/details/70244002