The previous few spoke of the custom control's combined control, address: http://www.cnblogs.com/zhangqie/p/8985612.html
Today's blog post is mainly about the viewgroup of custom controls.
What is ViewGroup?
ViewGroup is a container. It contains 0 or more view and sub-view
What role does ViewGroup have?
The ViewGroup can be used to hold multiple view controls and measure the view child control based on its own measurement mode, and to determine the location of the view child control. This will step you through how it measures and determines the size and position of the child controls.
Custom controls
Public classFlowlayoutbextendsViewGroup {Private inthorizontolspacing; Private intverticalspacing; PublicFlowlayoutb (Context context) {Super(context); Init (context); } PublicFLOWLAYOUTB (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); Init (context); } PrivateLine CurrentLine;//the current row Private intusewidth = 0;//width used by the current row PrivateList<line> Mlines =NewArraylist<line>(); Private intwidth; PublicFlowlayoutb (Context context, AttributeSet attrs) {Super(context, attrs); Init (context); } Private voidinit (Context context) {horizontolspacing= util.dip2px (13, context); Verticalspacing= util.dip2px (13, context); } //measure the current control FlowLayout//the parent class is obliged to measure each child view's@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {mlines.clear (); CurrentLine=NULL; Usewidth= 0; /*** Obtain this viewgroup superior container for its recommended width and height, as well as the calculation mode*/ intWidthmode =Measurespec.getmode (WIDTHMEASURESPEC); intHeightmode =Measurespec.getmode (HEIGHTMEASURESPEC); //Calculate the width and height of all ChildviewMeasurechildren (Widthmeasurespec, Heightmeasurespec); Width= Measurespec.getsize (Widthmeasurespec)-getpaddingleft ()-getpaddingright (); intHeight = measurespec.getsize (heightmeasurespec)-Getpaddingbottom ()-getpaddingtop ();//get to width and height intChildewidthmode; intChildeheightmode; //Each sub-view measurement rule needs to be specified in order to measure each sub-viewChildewidthmode = Widthmode = = measurespec.exactly?MeasureSpec.AT_MOST:widthMode; Childeheightmode= Heightmode = = measurespec.exactly?MeasureSpec.AT_MOST:heightMode; intChildwidthmeasurespec =Measurespec.makemeasurespec (Childewidthmode, width); intChildheightmeasurespec =Measurespec.makemeasurespec (childeheightmode, height); CurrentLine=NewLine ();//The first line is created for(inti = 0; I < Getchildcount (); i++) {View child=Getchildat (i); //measure each sub viewchild.measure (Childwidthmeasurespec, Childheightmeasurespec); intMeasuredwidth =child.getmeasuredwidth (); Usewidth+ = Measuredwidth;//make the current line plus the length of the use if(Usewidth <=width) {Currentline.addchild (child);//This is the time to prove that the current child view can be put into the current line and put inUsewidth + =horizontolspacing; } Else { //line BreakNewLine (child); } } if(!mlines.contains (CurrentLine)) {Mlines.add (currentline);//Add last line } intTotalheight = 0; for(line line:mlines) {totalheight+=line.getheight (); } totalheight+ = Verticalspacing * (mlines.size ()-1) + getpaddingtop () +Getpaddingbottom (); System.out.println (Totalheight); Setmeasureddimension (Width+ getpaddingleft () +getpaddingright (), Resolvesize (Totalheight, Heightmeasurespec)); } Private voidNewLine (View child) {Mlines.add (currentline);//record the previous lineCurrentLine =NewLine ();//Create a new rowCurrentline.addchild (child); Usewidth=Currentline.linewidth; } Private classLine {intHeight = 0;//the height of the current row intLineWidth = 0; Privatelist<view> children =NewArraylist<view>(); /*** Add a Sub view * *@param Child*/ Public voidAddChild (View child) {children.add (child); if(Child.getmeasuredheight () >height) {Height=child.getmeasuredheight (); } linewidth+=child.getmeasuredwidth (); } Public intgetheight () {returnheight; } /*** Returns the number of child view * *@return */ Public intGetchildcount () {returnchildren.size (); } Public voidLayoutintLintt) {linewidth+ = Horizontolspacing * (children.size ()-1); intSurpluschild = 0; intSurplus = width-linewidth; if(Surplus > 0 && children.size () > 0) {Surpluschild= Surplus/children.size (); } for(inti = 0; I < children.size (); i++) {View child=Children.get (i); Child.layout (L, T, l+ child.getmeasuredwidth () + surpluschild, T +child.getmeasuredheight ()); L+ = Child.getmeasuredwidth () +Surpluschild; L+=horizontolspacing; } } } //assigning the location of each child view@Overrideprotected voidOnLayout (BooleanChangedintLintTintRintb) {L+=Getpaddingleft (); T+=Getpaddingtop (); for(inti = 0; I < mlines.size (); i++) { line line=Mlines.get (i); Line.layout (L, T); //given to each line to allocateT + = line.getheight () +verticalspacing; } }}
Steps to customize ViewGroup:
- Inheriting ViewGroup, overriding construction methods
- Override the Onmeasure method to measure the child control and its own width height
- Implementing the OnLayout method placing child controls
:
Source Address: Https://github.com/DickyQie/android-custom-control
Resources:
48055879
50533658
51089453
ViewGroup of Android--------custom controls