1. First, post the code that I tested successfully.
Copy Code code as follows:
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
int measurewidth = measurespec.getsize (Widthmeasurespec);
int measureheigth = measurespec.getsize (Heightmeasurespec);
Setmeasureddimension (Measurewidth, measureheigth);
TODO auto-generated Method Stub
for (int i= 0;i<getchildcount (); i++) {
View v = getchildat (i);
LOG.V (TAG, "Measurewidth is" +v.getmeasuredwidth () + "Measureheight is" +v.getmeasuredheight ());
int widthspec = 0;
int heightspec = 0;
Layoutparams params = V.getlayoutparams ();
if (Params.width > 0) {
Widthspec = Measurespec.makemeasurespec (Params.width, measurespec.exactly);
}else if (params.width = = 1) {
Widthspec = Measurespec.makemeasurespec (Measurewidth, measurespec.exactly);
else if (params.width = = 2) {
Widthspec = Measurespec.makemeasurespec (Measurewidth, measurespec.at_most);
}
if (Params.height > 0) {
Heightspec = Measurespec.makemeasurespec (Params.height, measurespec.exactly);
}else if (params.height = = 1) {
Heightspec = Measurespec.makemeasurespec (Measureheigth, measurespec.exactly);
else if (params.height = = 2) {
Heightspec = Measurespec.makemeasurespec (Measurewidth, measurespec.at_most);
}
V.measure (Widthspec, Heightspec);
}
}
explain:
First, determine the value of params.width, there are three of cases.
If it is greater than 0, and the transmission is a specific value, then the construction of Measuprespec can be directly used exactly.
If 1 is matchparent, then the width of the parent view is obtained, and the exactly is used to construct the Measurespec.
If 2 is the case of wrapcontent, then it is possible to construct measurespec directly with a negative number.