The onCreate function only provides the opportunity for data initialization, and no formal drawing is yet made. If the image has not been rendered, the value of getWidth () or getHeight () is 0. To adapt to the screen, you need to enter the interface to obtain the size of the parent control, and then you need to dynamically set the size of the Child control in onCreate.
Method 1: Use the addOnPreDrawListener interface of the ViewTreeObserver class. (Good) ViewTreeObserver vto = hscrollview. getViewTreeObserver ();
Vto. addOnPreDrawListener (newViewTreeObserver. OnPreDrawListener (){
@ Override
Publicboolean onPreDraw (){
If (hasMeasured = false ){
Intwid = hscrollview. getWidth ();
Inthei = hscrollview. getHeight ();
If (wid! = 0 & hei! = 0 ){
HasMeasured = true;
}
}
Returntrue;
}
});
Method 2: Use the addOnGlobalLayoutListener interface of the ViewTreeObserver class. (Good)
Similar to method 1.
Method 3: Set the control size after the postDelayed delay is fixed. (Not good)