Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
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
- 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 gets to the view that the program does not include the title bar of the section, and then you can know the height of the title bar.
[Java]View PlainCopy
- int contenttop = GetWindow (). Findviewbyid (window.id_android_content). GetTop (); //statusbarheight is the height of the status bar as asked above int titlebarheight = Contenttop-statusbarheight
Example code:
[Java]View PlainCopy
- 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;PublicClass MainExtends Activity {ImageView IV;@Overridepublicvoid OnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (r.layout.main); iv = (ImageView)This.findviewbyid (R.ID.IMAGEVIEW01); Iv.post (New Runnable () {Publicvoid Run () {viewinited ();}}); LOG.V ("Test", "= = OK = =");} private void viewinited () {rect 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 result: OK after more than 100 ms Run LOG.V ("Test", "=-init-= statusbarheight=" + statusbarheight+ "contentviewtop=" + Contentview Top + "titlebarheight=" + Titlebarheight);}}
[XHTML]View PlainCopy
- <?Xmlversion="1.0"encoding="Utf-8"?><LinearLayoutXmlns:android="Http://schemas.android.com/apk/res/android"android:orientation= "vertical" android:layout_width="fill_parent" android:layout_height= "Fill_parent"><ImageView android:id="@+id/imageview01" android:layout_width= "Wrap_content" android:layout_height="wrap_content"/></linearlayout >
Android Gets the height of the status bar and title bar