The custom control will override the view's Onmeasure method, because it specifies the size of the control on the screen.
protected void onmeasure (intint heightmeasurespec)
Onmeasure the two parameters passed in are the size passed in by the previous control, and in many cases, overriding the method requires the actual size of the calculated control, and then calls setmeasureddimension (int, int) to set the actual size.
Onmeasure incoming Widthmeasurespec and Heightmeasurespec are not general dimensional values , but are values that combine patterns and dimensions.
Three functions commonly used , &NB Sp , &NB Sp
int getmode (int measurespec)// Extract mode according to the measured value (format) provided (one of the three modes above)int getsize ( int measurespec)// Extract the size value (this size is what we usually call the size) int Makemeasurespec According to the provided measurement (format) ( int size,int mode)// Create a measure value (format) based on the provided size value and mode
Get mode:
int mode = Measurespec.getmode (WIDTHMEASURESPEC)
Get Size:
int size = measurespec.getsize (Widthmeasurespec)
Mode
There are three scenarios in mode, with values of
Measurespec.unspecified, measurespec.exactly, Measurespec.at_most
- Measurespec.exactly is a precise size when we specify a control's Layout_width or Layout_height as a specific value, such as andorid:layout_width= "50dip", or Fill_ The parent is a case where the size of the control has been determined, all of which are exact dimensions.
- Measurespec.at_most is the maximum size, and when the control's layout_width or layout_height is specified as wrap_content, the size of the control generally varies with the control's subspace or content. The size of the control can be as long as it does not exceed the maximum allowable size of the parent control. Therefore, mode at this point is at_most,size gives the maximum size allowed by the parent control.
- Measurespec.unspecified is not a specified size, this is not a lot, it is generally the parent control is Adapterview, passed through the measure method of the mode.
Code
The controls are self-adapting according to the portrait and screen size:
@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); intwidth =measurespec.getsize (WIDTHMEASURESPEC); intHeight =measurespec.getsize (HEIGHTMEASURESPEC); if(0 = = Mratiowidth | | 0 = =mratioheight) {setmeasureddimension (width, height); } Else { if(Width < Height * Mratiowidth/mratioheight) {setmeasureddimension (width, Width* Mratioheight/mratiowidth); } Else{setmeasureddimension (height* Mratiowidth/mratioheight, height); } } } Public voidFitwindow (intWidthintheight) { if(Width < 0 | | Height < 0) { Throw NewIllegalArgumentException ("Size cannot be negative.")); } mratiowidth= width;//Screen WidthMratioheight = height;//Screen HeightRequestlayout ();//prompting call Onmeasure}I'm the dividing line of the king of the land Tiger.
Android--Measurespec