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