Android obtains the coordinates of custom controls, screen size, title bar, Status Bar Height, and android status bar.

Source: Internet
Author: User

Android obtains the coordinates of custom controls, screen size, title bar, Status Bar Height, and android status bar.
Android obtains the coordinates of custom controls, screen size, title bar, and Status Bar Height.

1. Get the height of the custom control

Obtain the height of the control in the current Activity in this Activity:

Button button = (Button)findViewById(R.id.button);int buttonHeight = button.getHeight();

Get the height of the control in other xml files in Activity:

LayoutInflater factorys = LayoutInflater. from (current class. this); final View textEntryView = factorys. inflate (R. layout. another XML, null); TextView y_type = (TextView) textEntryView. findViewById (R. id. control ID); int textHeight = y_type.getHeight ();
Or
LayoutInflater inflater = (LayoutInflater) context. getSystemService (LAYOUT_INFLATER_SERVICE); final View layout = inflater. inflate (R. layout. my_kebiao_setting, null); TextView y_type = (TextView) findViewById (R. id. control ID); int textHeight = y_type.getHeight ();

2. Obtain the screen size:

DisplayMetrics metrics = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (metrics); int screenWidth = metrics. widthPixels; // screen width int. screenHeight = metrics. heightPixels; // screen height

This code can be inserted into the onCreate () function of the Activity.


3. Obtain the title bar and Status Bar Height:

Rect rect = new Rect (); Window win = this. getWindow (); win. getDecorView (). getWindowVisibleDisplayFrame (rect); int statusBarHeight = rect. top; int contentViewTop = win. findViewById (Window. ID_ANDROID_CONTENT ). getTop (); int titleBarHeight = contentViewTop-Variable. statusBarHeight; // statusBarHeight indicates the height of the status bar and titleBarHeight indicates the height of the title bar.

GetLocationOnScreen calculates the x and y values of the view in the global coordinate system. (Note that the value is counted from the top of the screen, that is, the height of the notification bar) // obtain the absolute coordinates on the current screen
GetLocationInWindow: calculates the x and y coordinates of the view's widnow, and // obtains the absolute coordinates of the view within the window.

GetLeft, getTop, getBottom, and getRight are the coordinates of the parent group.


Int [] location = new int [2]; view. getLocationInWindow (location); // obtain the absolute coordinate view in the current window. getLocationOnScreen (location); // obtain the absolute coordinates of location [0] ---> x coordinate, location [1] ---> y coordinate throughout the screen





How does Android obtain the system height, title bar, and Status Bar Height?

GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (rect); // obtain the entire view. Note that if you want to set the title style, this must appear after the title style; otherwise, an error occurs: int top = rect. top; // The height of the status bar, so rect. height, rect. width is the width of the system height. View v = getWindow (). findViewById (Window. ID_ANDROID_CONTENT); // obtain the Root View int top2 = v. getTop (); // the total height of the title bar of the status bar, so the height of the title bar is top2-topint width = v. getWidth (); // The width of the view, which is always the largest int height = v. getHeight (); // view height, excluding the status bar and title bar. If you only want to obtain the screen size, you can use Display display = getWindowManager (). getdefadisplay display ();

Hi, how can I obtain the Title Bar Height of Android? QQ: 136165000

To solve this problem, you can consider the style, that is, define your own style in XML. You can view the relevant information in the SDK documentation. This is also an example in Google's own Demo, you can refer to the implementation. Define multiple styles

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.