Android Wrap-Wrapping layout

Source: Internet
Author: User

ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/***/ Public classAutowraplayoutextendsViewGroup {
The maximum number of rows supported is 10 rows, which can be adjusted as neededPrivate int[] Mrawarray =New int[10]; Private intmpadding = 3; PublicAutowraplayout (Context context) { This(Context,NULL); } PublicAutowraplayout (Context context, AttributeSet attrs) { This(context, Attrs, 0); } PublicAutowraplayout (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); intwidth =getmeasuredwidth (); intHeight =getmeasuredheight (); intChildCount =Getchildcount (); for(inti = 0; i < ChildCount; i++) {View child=Getchildat (i); Child.measure (measurespec.unspecified, measurespec.unspecified); } intRawchildwidthsum = 0; intMaxchildheight = 0; for(inti = 0, raw = 0; i < ChildCount; i++) {View child=Getchildat (i); if(Child.getvisibility ()! =GONE) { intChildwidth =child.getmeasuredwidth (); intChildheight =child.getmeasuredheight (); if(Maxchildheight < childheight) Maxchildheight =Childheight; Rawchildwidthsum+=Childwidth; Mrawarray[raw]=i; if(Rawchildwidthsum >width) {Mrawarray[raw]--; Raw++; Rawchildwidthsum= 0; } Mrawarray[raw]=i; } } intAllrawheight = 0; for(intArawarray:mrawarray) { if(Arawarray > 0) {Allrawheight+ = (maxchildheight + mpadding * 3); } else {
Break
}
} setmeasureddimension (width, allrawheight); } @Overrideprotected voidOnLayout (BooleanChangedintLintTintRintb) {intleft = 0; inttop =T; intMaxchildheight = 0; for(inti = 0; i < mrawarray.length; i++) {//every raw//I <=> raw number intArawarray =Mrawarray[i]; //Handle first Raw particularly. if(i = = 0) { if(Arawarray > 0) { for(intj = 0; J <= Arawarray; J + +) {//column FinalView Child =Getchildat (j); intChildwidth =child.getmeasuredwidth (); intChildheight =child.getmeasuredheight (); Left+=Childwidth; Top= 0; if(Maxchildheight < childheight) Maxchildheight =Childheight; Child.layout ( Left-Childwidth, 2, left, top + maxchildheight + 3); } } Continue; } if(Arawarray > 0) { left= 0; for(intj = mrawarray[i-1] + 1; J <= Arawarray; J + +) {//column FinalView Child =Getchildat (j); intChildwidth =child.getmeasuredwidth (); intChildheight =child.getmeasuredheight (); Left+=Childwidth; Top= i * maxchildheight + maxchildheight +T; if(Maxchildheight < childheight) Maxchildheight =Childheight; Child.layout ( Left-Childwidth, Top-maxchildheight + 2, left, top + 3); } } Else { Break; } } }}

Android Wrap-Wrapping layout

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.