Where to get view in Android app

Source: Internet
Author: User

1. Relative Position:

GetLeft (), GetRight (), GetTop (), Getbottom ()

In Android, you can put left equivalent to the x-axis value, top equivalent to the y-axis value, through the two values of the Android system can know the view of the drawing starting point, in the Wdith and Height can get the view up and down the specific value, you can on the screen absolute position drawing view. Right and bottom are calculated as follows:

right = left + width;

Bottom = top + height;

View left position View.getleft ()

View right position view.getright ()

The top position of the view view.gettop ();

View bottom position view.getbottom ();

View width view.getwidth ();

View Height view.getheight ()

Wrong understanding, the location of the green area is for the blue area of the (0, 0) coordinates of the value, from the lower right corner of the printed coordinate values can be seen with the below I listed values inconsistent,

Just look at the picture below.

Blue Area Position left = 0, top = 0 coordinates (0, 0)

Yellow area Position left =, top = 115 coordinates (60, 115)

Green Zone Position left = 170 coordinates (115, 170)

Summary: The left, top, right, bottom values of the view are relative to their parent view, and the green area is for the coordinates of its parent view (that is, the yellow area is (0, 0) points) and should not be (115, 170) but (55, 55)

2. Absolute position:

    1. Getlocalvisiblerect
    2. Getglobalvisiblerect
    3. Getlocationonscreen
    4. Getlocationinwindow

1) Getlocationinwindow

    1. int[] Position = new INT[2];

    2. Textview.getlocationinwindow (position);

    3. System.out.println ("Getlocationinwindow:" + position[0] + "," + position[1]);

This method stores the upper-left coordinate of the view in an array. This coordinate is relative to the current activity.

For normal activity, the y-coordinate is the visible status bar height + visible title bar height +view the upper-left corner to the bottom of the title bar.

In the case of hidden status bar/title bar, their height is calculated at 0.

For dialog-style activity, the y-coordinate is the distance from the visible title bar height +view to the bottom of the title bar.

At this point there is no disregard for the status bar.

2) Getlocationonscreen

    1. int[] Position = new INT[2];

    2. Textview.getlocationonscreen (position);

    3. System.out.println ("Getlocationonscreen:" + position[0] + "," + position[1]);

This method is similar to the above, and it also stores the upper-left corner of the view in the array. But this coordinate is relative to the entire screen.

The y-coordinate is the distance from the upper-left corner of the view to the top of the screen.

3) Getglobalvisiblerect

    1. Rect viewrect = new rect ();

    2. Textview.getglobalvisiblerect (Viewrect);

    3. System.out.println (Viewrect);

The method is to construct a rect to "set" the view. The coordinates of this rect are relative to the current activity.

For normal activity, the top of the rect is the visible status bar height + the distance from the upper left corner to the bottom of the title bar of the visible title bar height +rect.

For dialog-style activity, the y-coordinate is the top of the rect that is visible, the distance from the upper-left corner to the bottom of the title bar for the +rect height of the title bar.

At this point there is no disregard for the status bar.

4) Getlocalvisiblerect

    1. Rect globerect = new rect ();

    2. Button.getlocalvisiblerect (Globerect);

Attention:

The above method is called in the OnCreate method, which returns 0 because the view is not loaded.

It is recommended to obtain in the Onwindowfocuschanged method, in some cases onwindowfocuschanged when it is not good (such as Activitygroup), you can write:

    1. Mtextview.post (New Runnable () {

    2. @Override

    3. public void Run () {

    4. Rect viewrect = new rect ();

    5. Mtextview.getlocalvisiblerect (Viewrect);

    6. Mtreescrollview.setrect (Viewrect);

    7. }

    8. });

This will perform the method of acquiring the location after the view has finished loading.

Where to get view in Android app

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.