Android gets screen height, caption height, status bar height (instance code) _android

Source: Internet
Author: User

There are two ways to get a height from a view-provided method:

1, the currently displayed view directly gets the current view width
2, through the activity of GetWindow (). Findviewbyid (window.id_android_content) Gets the view root (is a Framelayout object) currently displayed by the system. The Android drawing will be drawn in the framelayout with the view that will be drawn.

The display object gets the screen width:



Gets the Getwindowmanager (). Getdefaultdisplay () in the Display object activity.
getwidth () returns the width of the display interface, which is the screen width
getheight () Returns the height of the display interface, which is the screen height

Setting the Displaymetrics width value by the display object and getting the screen wide by the Displaymetrics object is a bit superfluous:
getwidth () returns the width of the display interface, which is the screen width
getheight () Returns the height of the display interface, which is the screen height

Some value calculations are commonly used:

High screen width
Canvas objects, display objects, and displaymetrics to get a high screen width

Status bar Height
View Getwindowvisibledisplayframe (Rect outrect) appended Outrect, Outrect.top () is the status bar height

Title Height
The Outrect.height ()-view.getheight () is the caption height after the getwindowvisibledisplayframe (Rect outRect1) value outrect The view is attached.

1. Get Status bar Height:

Decorview is the topmost view in the window, you can get Decorview from windows, and then Decorview has a getwindowvisibledisplayframe method to get to the area that the program displays, including the title bar , but the status bar is not included.

So, we can figure out the height of the status bar.

Copy Code code as follows:

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, and then you know the height of the title bar.

Copy Code code as follows:

int contenttop = GetWindow (). Findviewbyid (window.id_android_content). GetTop ();
Statusbarheight is the height of the status bar above
int titlebarheight = Contenttop-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.