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;
}