Get the height of the window status bar and title bar

Source: Internet
Author: User

1. Get the status bar height:

Decorview is the topmost view in window, you can get to decorview from window, and Decorview has a getwindowvisibledisplayframe method to get to the area displayed by the program, including the title bar , but does not include the status bar.

So we can figure out the height of the status bar.

[Java]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. Rect frame = new Rect ();
    2. GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
    3. int statusbarheight = Frame.top;

2. Get the title bar height:

GetWindow (). Findviewbyid (Window.id_android_content) This method can get to the part of the view that does not include the title bar, and then you can know the height of the title bar.

[Java]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. int contenttop = GetWindow (). Findviewbyid (window.id_android_content). GetTop ();
    2. Statusbarheight is the height of the status bar that is asked above
    3. int titlebarheight = Contenttop-statusbarheight; (Statusbarheight see above)


As to the above point, since the window view is drawn with a certain delay, it is not possible to use the above method directly from OnCreate ()->onresume () in the visible phase of the window when acquiring the height of the status bar. There are 3 ways to solve this problem:
1. The onclick () method can be placed under the onclicklistener of a button;
2. Of course you can also get in OnPause ()->ondestroy ()
3. Use a handler. The test code is as follows:
Inside the OnCreate () method, execute:
Mhandler.postdelayed (R, 200);

[Java]View Plaincopy
  1. Runnable r = New Runnable () {
  2. @Override
  3. public Void Run () {
  4. Rect frame = new Rect ();
  5. GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
  6. int statusbarheight = frame.top;
  7. LOG.D (TAG, "&&&&" + statusbarheight);
  8. }
  9. };

The last suggestion is to get this value only once, and then store it, similar to the high-width gain of the screen, and reduce the duplication of system resources by using too much.

Transferred from: http://blog.csdn.net/androidzhaoxiaogang/article/details/6825163#

Get the height of the window status bar and title bar

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.