Android get custom control position coordinates, screen size, title bar, status bar height

Source: Internet
Author: User

Android get custom control position coordinates, screen size, title bar, status bar height

1. Get the custom control height

In this activity, get the height of the control in the current activity:

Button button = (button) Findviewbyid (R.id.button), int buttonheight = Button.getheight ();

Gets the height of the control in the other XML file in activity:

Layoutinflater Factorys = Layoutinflater.from (current class. This); final View Textentryview = Factorys.inflate ( R.layout. You want to get 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. Get the screen size:

Displaymetrics metrics = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics); int screenwidth=metrics.widthpixels;            Screen width int. screenheight=metrics.heightpixels;        Screen height

This code can be inserted into the activity's OnCreate () function.


3. Get the title bar, 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 is the status bar height, Titlebarheight is the title bar height

Getlocationonscreen calculates the x, Y value of the view in the global coordinate system (note that the value is calculated from the top of the screen, which includes the height of the notification bar)//Gets the absolute coordinates in the current screen
Getlocationinwindow calculates the x, Y value of the view in its widnow coordinates,//Gets the absolute coordinates within the entire window

GetLeft, GetTop, Getbottom, GetRight, this group is getting the coordinates relative to its father


int[] location = new  int[2]; View.getlocationinwindow (position);//Gets the absolute coordinates within the current window View.getlocationonscreen ( location);//get absolute coordinates within the entire screen location [0]--->x coordinates, location [1]--->y coordinates




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.