Parsing: How to override the Onmeasure method after inheriting the ViewGroup subclass _android

Source: Internet
Author: User
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.

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.