Retrieve the Status Bar Height
1. Traditional Method: Sometimes the value is 0. solution 2:
Code
Rect frame = new Rect ();
GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame );
Int statusBarHeight = frame. top;
II. The value may be 0 after 4.0.3.
Public int getBarHeight (){
Class <?> C = null;
Object obj = null;
Field field = null;
Int x = 0, sbar = 38; // The default value is 38. It seems that most of them are like this.
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 ());
Sbar = getResources (). getDimensionPixelSize (x );
} Catch (Exception e1 ){
E1.printStackTrace ();
}
Return sbar;
}
//---------------------------------------------
1. Get the status bar height:
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.
Code
Rect frame = new Rect ();
GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame );
Int statusBarHeight = frame. top;
--------------------------------------------------------------------------------
Sometimes the obtained height is 0, which can be obtained in another way.
Obtain the status bar height code in the source code program:
Height = getResources (). getDimensionPixelSize (com. android. internal. R. dimen. status_bar_height );
6. Code
Class c = Class. forName ("com. android. internal. R$ dimen ");
Object obj = c. newInstance ();
Field field = c. getField ("status_bar_height ");
Int x = Integer. parseInt (field. get (obj). toString ());
Int y = getResources (). getDimensionPixelSize (x );
2. Obtain the title bar height:
The view obtained by the getWindow (). findViewById (Window. ID_ANDROID_CONTENT) method is the part of the title bar that the program does not contain. Then you can know the height of the title bar.
Code
Int contentTop = getWindow (). findViewById (Window. ID_ANDROID_CONTENT). getTop ();
// StatusBarHeight is the height of the status bar requested above
Int titleBarHeight = contentTop-statusBarHeight
3. Get screen height
Method 1.
Java code
Code
WindowManager windowManager = getWindowManager ();
Display display = windowManager. getDefaultDisplay ();
ScreenWidth = display. getWidth ();
ScreenHeight = display. getHeight ();
Method 2.
Java code
DisplayMetrics dm = new DisplayMetrics ();
This. getWindowManager (). getDefaultDisplay (). getMetrics (dm); // this indicates the current activity
ScreenWidth = dm. widthPixels;
ScreenHeight = dm. heightPixels;
The above two methods are still in the 0 state when the screen is not displayed, that is, they are valid only after setContentView is called.
Set to untitled
RequestWindowFeature (Window. FEATURE_NO_TITLE );
Set to full screen mode getWindow (). setFlags
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN );
Set to landscape
SetRequesteOrientation (ActivityInfo. SCREEN_ORIENTATION_LADSCAPE );