[Android] Correctly Setting the size of custom components and their internal components

Source: Internet
Author: User

1. No matter how you set the size of custom components, the custom components always seem to be match_parent. We need to override the onMeasure method and correctly set the component size in it. [Java] @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {// Note: to be as concise as possible, this article only discusses EXACTLY and AT_MOST modes int mode = MeasureSpec. getMode (widthMeasureSpec); if (mode = MeasureSpec. AT_MOST | mode = MeasureSpec. EXACTLY) {this. widthMeasureSpec = widthMeasureSpec; this. heightMeasureSpec = heightMeasureSpec; int width = MeasureSpec. getSize (widthMeasureSpec); int height = MeasureSpec. g EtSize (heightMeasureSpec); setMeasuredDimension (width, height);} else if (mode = MeasureSpec. UNSPECIFIED) {Log. d ("WOGU", "mode = UNSPECIFIED"); super. onMeasure (widthMeasureSpec, heightMeasureSpec);} 2. Set the size of the component in the Custom component to overwrite onMeasure and onLayout, and set the size of the child element in the component. [Java] public class MyViewGroup extends ViewGroup {protected int widthMeasureSpec, heightMeasureSpec; @ Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) {if (changed) {View view = getChildAt (0); // measure triggers onMeasure of the View. // WidthMeasureSpec includes size and mode // int width = MeasureSpec. getSize (widthMeasureSpec); // int height = MeasureSpec. getSize (heightMeasureSpec); // int mode = MeasureSpec. getMode (widthMeasureSpec); // the possible value of mode: MeasureSpec. AT_MOST, MeasureSpec. EXACTLY, MeasureSpec. UNSPECIFIED // here, we simply record widthMeasureSpec and heightMeasureSpec in onMeasure // In fact, MeasureSpec provides a method to synthesize widthMeasureSpec and heightMeasureSpec according to the sizet and pattern. makeMeasureSpec (getWidth (), MeasureSpec. EXACTLY); // int heightMeasureSpec = MeasureSpec. makeMeasureSpec (getHeight (), MeasureSpec. EXACTLY); view. measure (widthMeasureSpec, heightMeasureSpec); view. layout (0, 0, getWidth (), getHeight () ;}@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {this. widthMeasureSpec = widthMeasureSpec; this. heightMeasureSpec = heightMeasureSpec ;...}}

Related Article

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.