Two methods are used to obtain the status bar height f, and two methods are used to obtain the status bar

Source: Internet
Author: User

Two methods are used to obtain the status bar height f, and two methods are used to obtain the status bar
Method 1. 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 plain



  • Rect frame = new Rect ();
  • GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame );
  • Int statusBarHeight = frame. top;
  • Method 2:
  • Public static int getStatusBarHeight (Context context ){

    Class <?> C = null;

    Object obj = null;

    Field field = null;

    Int x = 0, statusBarHeight = 0;

    Try {

    C = Class. forName ("com. android. internal. R$ dimen ");

    Obj = c. newInstance ();

    Field = c. getField ("status_bar_height ");

    X = Integer. parseInt (field. get (obj). toString ());

    StatusBarHeight = context. getResources (). getDimensionPixelSize (x );

    } Catch (Exception e1 ){

    E1.printStackTrace ();

    }

    Return statusBarHeight;

    }


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.