In Android, sometimes it is necessary to measure the control, and the resulting control width and height can be used to do some calculations. This calculation is particularly important when adaptive screens are required. Another convenience is that due to requirements, it is hoped that the width and height of the control can be obtained after entering the interface.
Unfortunately, according to my verification, the method reprinted online is still 0 in the OnCreate function (I hope that those who work on the technology can verify it themselves and then reprint it). For example, the value of the getMeasuredWidth is still 0 after calling the Measured method.
The reason is that when the OnCreate function occurs, it only provides an opportunity to initialize data, and the graph has not been officially drawn at this time. The drawing of the figure is performed in OnDraw, and the calculation seems too late at this time. The easy way to think of is: hope that after the program has just measured a specified control, it can immediately calculate or initialize its width and height. This requires a method to listen to the occurrence of this event. Fortunately, Android provides such a mechanism. Using the getViewTreeObserver method in the View class, you can get the observer of the specified View and make a callback the moment before drawing the control. This way, the data obtained is accurate, but this method may be called repeatedly afterwards, so it needs to be added. Under ordinary requirements, it is enough to calculate it once. The code is as follows (this code is verified in the OnCreate callback function. In real time, because it is a listener, the event has nothing to do with OnCreate when it occurs):
layout = (MetroLayout) findViewById();
ViewTreeObserver vto = ();
(new ()
{
public boolean onPreDraw()
{
if (hasMeasured == false)
{
int height = ();
int width = ();
//After obtaining the width and height, it can be used for calculation
hasMeasured = true;
}
return true;
}
});
Unfortunately, according to my verification, the method reprinted online is still 0 in the OnCreate function (I hope that those who work on the technology can verify it themselves and then reprint it). For example, the value of the getMeasuredWidth is still 0 after calling the Measured method.
The reason is that when the OnCreate function occurs, it only provides an opportunity to initialize data, and the graph has not been officially drawn at this time. The drawing of the figure is performed in OnDraw, and the calculation seems too late at this time. The easy way to think of is: hope that after the program has just measured a specified control, it can immediately calculate or initialize its width and height. This requires a method to listen to the occurrence of this event. Fortunately, Android provides such a mechanism. Using the getViewTreeObserver method in the View class, you can get the observer of the specified View and make a callback the moment before drawing the control. This way, the data obtained is accurate, but this method may be called repeatedly afterwards, so it needs to be added. Under ordinary requirements, it is enough to calculate it once. The code is as follows (this code is verified in the OnCreate callback function. In real time, because it is a listener, the event has nothing to do with OnCreate when it occurs):
Copy the codeThe code is as follows:
layout = (MetroLayout) findViewById();
ViewTreeObserver vto = ();
(new ()
{
public boolean onPreDraw()
{
if (hasMeasured == false)
{
int height = ();
int width = ();
//After obtaining the width and height, it can be used for calculation
hasMeasured = true;
}
return true;
}
});