Android----------WindowManager

Source: Internet
Author: User

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
  1. WindowManager mWm = (windowmanager) getsystemservice (Context.window_service);
  2. Button view = new button (this);
  3. View.settext ("window manager test!");
  4. Windowmanager.layoutparams mparams = new windowmanager.layoutparams ();
  5. Mwm.addview (view, mparams);

Java code
  1. //Change dialog background Transparency
  2. Dialog dg = new Dialog (this);
  3. window window = Dg.getwindow ();
  4. Windowmanager.layoutparams LP = Window.getattributes ();
  5. Lp.alpha = 0. 5f;
  6. Window.setattributes (LP);
  7. //lp.alpha = 0.5f Transparency set its value to be reasonable to test repeatedly
  8. //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
    1. width = Activity.getwindowmanager (). Getdefaultdisplay (). GetWidth ();
    2. Height = Activity.getwindowmanager (). Getdefaultdisplay (). GetHeight ();

Android----------WindowManager

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.