Android Check Gaps (view)-Wrap_content Invalid solution in Custom view

Source: Internet
Author: User

Solution wrap_content Invalid in custom View

Children's shoes with custom view will find that a custom control that inherits the view directly needs to override the Onmeasure () method and set its own size when wrap_content, and if the custom control is set to a size in the layout file, Wrap_content is equivalent to Match_parent.

In fact, the Onmeasure () method has been rewritten in the control of Android, and the wrap_content and so on have been specially handled, and the default width and height are given when wrap_content. So for the treatment of this problem we also have a certain idea, in Onmeasure () for the wrap_content situation to give the appropriate width, height can, code as follows:

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(widthMeasureSpec, heightMeasureSpec);    int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);    int heithtSpecSize = MeasureSpec.getSize(heightMeasureSpec);    if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {        setMeasuredDimension(200, 200);    } else if (widthSpecMode == MeasureSpec.AT_MOST) {        setMeasuredDimension(200, heithtSpecSize);    } else if (heightSpecMode == MeasureSpec.AT_MOST) {        setMeasuredDimension(widthSpecSize, 200);    }}

Android Check Gaps (view)-Wrap_content Invalid solution in Custom view

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.