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" >
- Rect frame = new Rect ();
- GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
- 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" >
- int contenttop = GetWindow (). Findviewbyid (window.id_android_content). GetTop ();
- Statusbarheight is the height of the status bar that is asked above
- 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
- Runnable r = New Runnable () {
- @Override
- public Void Run () {
- Rect frame = new Rect ();
- GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
- int statusbarheight = frame.top;
- LOG.D (TAG, "&&&&" + statusbarheight);
- }
- };
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