Get the Android status bar height in a tried and tried way

Source: Internet
Author: User

As shown in the following code:

[Java]View Plaincopy
  1. private int   Getstatusbarheight ()  {  
  2.          class<?> c = null ;  
  3.   
  4.          object obj = null ;  
  5.   
  6.          field field = null ;  
  7.   
  8.          int  x =   0 , sbar =   0 ;  
  9.   
  10.          try  {   
  11.   
  12.              c = class.forname ( "Com.android.internal.r$dimen"
  13. obj = C.newinstance ();
  14.   
  15.              field = c.getfield ( "Status_bar_height" ) ;   
  16. x = Integer.parseint (Field.get (obj). toString ());
  17. Sbar = GetContext (). Getresources (). getdimensionpixelsize (x);
  18.   
  19.          } catch
  20. E1.printstacktrace ();
  21. }
  22.   
  23.          return  sbar;   
  24. }

This is a way for a cow to come up with. The method is a bit two, the main principle is to find dimen from the system R file, the inner class, and then through the reflection to get dimen in the value of Status_bar_height, this value is actually the resource ID, and then through the GetResource method to get the value corresponding to the ID, It's so perfect, it's impeccable! This method should also apply to the values of other system fields.


There are many ways to introduce the status bar on the network, but there are some limitations. Like what:

Status Bar Height
After the Getwindowvisibledisplayframe (Rect outrect) value of the view is Outrect, Outrect.top () is the status bar height

Title Height
Outrect.height ()-view.getheight () is the caption height after the view's Getwindowvisibledisplayframe (Rect outRect1) value Outrect.


This method only applies when the view has been drawn, so that the corresponding value can be obtained, and if I'm not mistaken, the view should also fill the screen. But in many cases we need to get this value before the view has been drawn, and this method is not reliable at all.


Source: >  

From for notes (Wiz)

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.