About Android View.getleft (), GetRight (), GetTop (), Getbottom () some doubts (pits) solution

Source: Internet
Author: User

Original

Today in the downturn when you hit the view of the four get function is a bit special, the specific problems encountered, after repeated testing and find information, pits as follows:

1, why do I sometimes use GetLeft (), GetRight (), GetTop (), Getbottom () The result they get is 0?

A: This may be the case when the program was just started, when the program started to draw the view, you immediately use the code to capture the above values. At this point, because the view is just beginning to draw, you will get 0.

To verify what I said above:

New thread, using the Thread.Sleep (long int) function to delay a period of time before using get, will not be 0.

2, why did I get the wrong value? For example, I set the height= "30DP" or 30dip of a control, and then use Getbottom (), do not get 30?

A: Unit problem. We're invoking the value of the four get function above the view, but it's a pixel, which is PX,

Conversion formula:

DP = (int) (px/1.5 + 0.5), DP is the same as dip.

How do 3,top, bottom and the other 2 compare?

A, the left of the view, top, right, the value of bottom is relative to its parent view, see figure.

Top and left are calculated directly, and the other 2 are as follows: drawings transferred from the network

right = left + width;

Bottom = top + height;

About Android View.getleft (), GetRight (), GetTop (), Getbottom () some doubts (pits) solution

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.