Android Gets the height of the status bar and title bar

Source: Internet
Author: User

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
    1. 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
    1. 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
  1. 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
  1. <?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

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.