Android gets the implementation code for the width and height of the control in OnCreate _android

Source: Internet
Author: User
In Android, controls are sometimes required to measure, and the resulting control width and height can be used for some calculations. This calculation is particularly important in situations where you need to adapt to the screen. Another convenience, because of the needs of the reason, I hope to enter the interface, you can get the width and height of the control.

Unfortunately, according to my verification, the use of the online reprint of those methods in the OnCreate function to get the still 0 (want to make the technology can verify their own reprint), such as the measure method after the call Getmeasuredwidth value or 0.

The reason is that when the OnCreate function occurs, it simply provides the opportunity to initialize the data, and there is no formal drawing at this time. The drawing is done in OnDraw, which is too late to compute. The easy way to think of it is that you want to be able to calculate or initialize data immediately after the program has just measured a specified control, and then get its width and height. This requires a way to monitor this event, but fortunately Android provides such a mechanism, using the Getviewtreeobserver method in the view class, to get to the viewer of the specified view and to recall the moment before the control is drawn, so that the speed does not delay , the data obtained is accurate, but this method may be called back and forth later, so it is necessary to add the limit, the general requirements, only one time is enough, the code is as follows (this code is validated in the OnCreate callback function, in real time, because it is the listener, So it has nothing to do with OnCreate when the event occurs):

Copy Code code as follows:

Layout = (metrolayout) Findviewbyid (r.id.layout);
Viewtreeobserver vto = Layout.getviewtreeobserver ();

Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener ()
{
Public boolean onpredraw ()
{
if (hasmeasured = = False)
{

int height = metrolayout.getmeasuredheight ();
int width = metrolayout.getmeasuredwidth ();
//Can be used to compute

Hasmeasured = True when the width and height are obtained;

}
return true;
}
});

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.