Our Android platform is composed of one activity after another and each activity has one or more view components. So, when we want to show an interface, the first thing we think about is building an activity, and then all the actions are implemented in the activity, or a dialog or toast. This approach is simple, but in some cases, we ask for a simple display, with activity is obviously superfluous, and at this time, how do we deal with it?
Originally, the entire android window mechanism is based on a called WindowManager, this interface can add view to the screen, you can also remove the view from the screen. It faces the object one end of the screen, the other end is view, directly ignoring our previous activity or dialog and other stuff. In fact, our activity or diolog bottom of the implementation is also through the WindowManager, this windowmanager is global, the whole system is the only thing. It shows the bottom of the view.
Write a simple code:
Java code
- WindowManager mWm = (windowmanager) getsystemservice (Context.window_service);
- Button view = new button (this);
- View.settext ("window manager test!");
- Windowmanager.layoutparams mparams = new windowmanager.layoutparams ();
- Mwm.addview (view, mparams);
Java code
- //Change dialog background Transparency
- Dialog dg = new Dialog (this);
- window window = Dg.getwindow ();
- Windowmanager.layoutparams LP = Window.getattributes ();
- Lp.alpha = 0. 5f;
- Window.setattributes (LP);
- //lp.alpha = 0.5f Transparency set its value to be reasonable to test repeatedly
- //Why do you want this technique because a lot of people say: Pop-up dialog will cause the background to darken and animation occur causing slow performance can be changed with this and can use this to make full transparency does not block the display of dialog back content
in general, when Android was first developed, it made a mistake to get getwidth () and GetHeight () in the view's constructor, and when a View object was created, Android didn't know its size, so getwidth () and GetHeight () Return the result is 0, the real size is calculated when the layout is calculated, so you will find an interesting thing, that is, in OnDraw () can achieve a long width reason.
Java code
- width = Activity.getwindowmanager (). Getdefaultdisplay (). GetWidth ();
- Height = Activity.getwindowmanager (). Getdefaultdisplay (). GetHeight ();
Android----------WindowManager