Android gets the height of the status bar and title bar

Source: Internet
Author: User

1. Get the status bar height:

Decorview is the top-level view in the window. You can obtain the decorview from the window. Then, decorview has a getwindowvisibledisplayframe method to obtain the display area of the program, including the title bar, but not the status bar.
So we can calculate the height of the status bar.

View plaincopy to clipboardprint?
01. rect frame = new rect ();
02. getwindow (). getdecorview (). getwindowvisibledisplayframe (FRAME );
03. Int statusbarheight = frame. Top;
Rect frame = new rect ();
Getwindow (). getdecorview (). getwindowvisibledisplayframe (FRAME );
Int statusbarheight = frame. Top;

2. Obtain the title bar height:

The view obtained by the getwindow (). findviewbyid (window. id_android_content) method is the part of the title bar that the program does not contain. Then you can know the height of the title bar.

View plaincopy to clipboardprint?
01. Int contenttop = getwindow (). findviewbyid (window. id_android_content). gettop ();
02. // statusbarheight is the height of the status bar requested above
03. Int titlebarheight = contenttop-statusbarheight
Int contenttop = getwindow (). findviewbyid (window. id_android_content). gettop ();
// Statusbarheight is the height of the status bar requested above
Int titlebarheight = contenttop-statusbarheight

Sample Code:

View plaincopy to clipboardprint?
01. Package com.cn. lhq;
02. Import Android. App. activity;
03. Import Android. Graphics. rect;
04. Import Android. OS. Bundle;
05. Import Android. util. log;
06. Import Android. View. window;
07. Import Android. widget. imageview;
08. Public class main extends activity {
09. imageview IV;
10. @ override
11. Public void oncreate (bundle savedinstancestate ){
12. Super. oncreate (savedinstancestate );
13. setcontentview (R. layout. Main );
14. IV = (imageview) This. findviewbyid (R. Id. imageview01 );
15. IV. Post (New runnable (){
16. Public void run (){
17. viewinited ();
18 .}
19 .});
20. log. V ("test", "= OK = ");
21 .}
22. Private void viewinited (){
23. rect = new rect ();
24. Window window = getwindow ();
25. IV. getwindowvisibledisplayframe (rect );
26. Int statusbarheight = rect. Top;
27. Int contentviewtop = Window. findviewbyid (window. id_android_content)
28. gettop ();
29. Int titlebarheight = contentviewtop-statusbarheight;
30. // Test Result: more than 100 MS run after OK
31. log. V ("test", "=-init-= statusbarheight =" + statusbarheight
32. + "contentviewtop =" + contentviewtop + "titlebarheight ="
33. + titlebarheight );
34 .}
35 .}
Package com.cn. lhq;
Import Android. App. activity;
Import Android. Graphics. rect;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. window;
Import Android. widget. imageview;
Public class main extends activity {
Imageview IV;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
IV = (imageview) This. findviewbyid (R. Id. imageview01 );
Iv. Post (New runnable (){
Public void run (){
Viewinited ();
}
});
Log. V ("test", "= OK = ");
}
Private void viewinited (){
Rect = new rect ();
Window window = getwindow ();
Iv. getwindowvisibledisplayframe (rect );
Int statusbarheight = rect. Top;
Int contentviewtop = Window. findviewbyid (window. id_android_content)
. Gettop ();
Int titlebarheight = contentviewtop-statusbarheight;
// Test results: OK after more than 100 MS to run
Log. V ("test", "=-init-= statusbarheight =" + statusbarheight
+ "Contentviewtop =" + contentviewtop + "titlebarheight ="
+ Titlebarheight );
}
}

View plaincopy to clipboardprint?
01. <? XML version = "1.0" encoding = "UTF-8"?>
02. <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
03. Android: Orientation = "vertical"
04. Android: layout_width = "fill_parent"
05. Android: layout_height = "fill_parent">
06. <imageview
07. Android: Id = "@ + ID/imageview01"
08. Android: layout_width = "wrap_content"
09. Android: layout_height = "wrap_content"/>
10. </linearlayout>

Http://blog.csdn.net/pilou5400/archive/2010/11/18/6018422.aspx

Related Article

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.